2014-09-02 27 views
1

你好我想颳了最新的電影www.allocine.fr初學者不知道如何加入名單,而刮

我做了下面的腳本:

# -*- coding: utf-8 -*- 
import urllib 
import re 

page = ["?page=1", "?page=2", "?page=3"] 

i=0 
while i<len(page): 
    url = "http://www.allocine.fr/film/aucinema/" +page[i] 
    htmlfile = urllib.urlopen(url) 
    htmltext = htmlfile.read() 

    regex = '<a class="no_underline" href="/film/fichefilm_gen_cfilm=[^.]*.html">\n(.+?)\n</a>' 

    pattern = re.compile(regex) 

    movie = re.findall(pattern,htmltext) 
    i+=1 
    movielist = '\n '.join(movie) 

    print movielist 

的問題是,第一併且列表中的最後一項在它們前面沒有空格...我想說的是輸出中第一個列表中的最後一個項目和第二個列表中的第一個項目不由空格分隔。

它看起來像這樣:

Something in 1st list 
something2 in 1st list 
something3 in 1st list 
Otherthing in 2nd list 
otherthing2 in 2nd list 
otherthing3 in 2nd list 

====

我想這是這樣的: 東西 東西 東西 otherthing otherthing

+0

另外對於網絡報廢,你可能想看看[BeautifulSoup](http://www.crummy.com/software/BeautifulSoup/bs4/doc/) – bvidal 2014-09-02 14:56:01

+0

只需添加它:'movielist =''+ movielist' – Germano 2014-09-02 14:58:35

回答

1

,你可以:

打印的空間之前:

movielist = ' ' + '\n '.join(movie) 

打印每個項目的空間:

movielist = '\n'.join([' ' +i for i in movie]) 

例:

>>> print '\n '.join(movie) 
something 
something 
something 
otherthing 
otherthing 
>>> print ' '+'\n '.join(movie) 
something 
something 
something 
otherthing 
otherthing 
>>> print '\n'.join([' ' +i for i in movie]) 
something 
something 
something 
otherthing 
otherthing 
+0

真棒,這正是我需要的!非常感謝你,它並沒有跨越我的想法做到這一點:D – 2014-09-02 15:19:32

+0

@AlexTheWebGroup酷。你可以接受/投票嗎? – valjeanval42 2014-09-02 15:28:48

0

,如果你只是想項目並列列表,然後將您的打印聲明更改爲類似print "foo" % bar,

參考: python print end=' '