2017-10-12 58 views
1

我正在爲數據抓取IMDB頁面,但是當試圖將其寫入CSV文件時,我只會從結果中獲取最後一行。Python腳本不在新行中寫入結果 - 新手

代碼如下:

from urllib.request import urlopen as uReq 
from bs4 import BeautifulSoup as soup 
my_url='http://www.imdb.com/search/title?genres=sci_fi&title_type=feature&sort=user_rating,desc' 
uClient = uReq(my_url) 
page_html=uClient.read() 
uClient.close() 
page_soup=soup(page_html,"html.parser") 

images=page_soup.findAll("div",{"class":"lister-item-image float-left"}) 

containers=page_soup.findAll("div",{"class":"lister-item-content"}) 

filename="scifi.csv" 
f=open(filename,"w") 

headers="order, title, year"'\n' 

f.write(headers) 

for container in containers: 
    number=container.h3.findAll("span",{"class":"lister-item-index unbold text-primary"}) 
    order=number[0].text 

    atitle=container.h3.findAll("a") 
    title=atitle[0].text 

    date=container.h3.findAll("span",{"class":"lister-item-year text-muted unbold"}) 
    year=date[0].text 
    print("order:" + order) 
    print("title:" + title) 
    print("year:" + year) 

f.write(order + "," +title+ "," +year + '\n') 
f.close() 

我使用蟒蛇& Spyder的。爲了在看YouTube之後對我的愛,在谷歌上搜索,我仍然不明白爲什麼在地獄裏它不是一個接一個地寫在所有的行上。謝謝!

+1

那麼,你只有一個'f.write(...)'調用,直接在你的for循環體外。所以,我懷疑這是原因。 –

+0

請顯示已完成的輸出以及您的代碼當前輸出的內容。使用調試器的結果是什麼?我也非常確定HTML和CSS標籤是無關緊要的,因爲你自己並不直接與他們合作,並且這些部分不太可能存在問題。 – tambre

+0

另外,你應該真的使用'csv'模塊創建csv的。 –

回答

1

你的f.write不在你的for循環中,所以它只寫了最後一行。這應該有效:

from urllib.request import urlopen as uReq 
from bs4 import BeautifulSoup as soup 
my_url='http://www.imdb.com/search/title?genres=sci_fi&title_type=feature&sort=user_rating,desc' 
uClient = uReq(my_url) 
page_html=uClient.read() 
uClient.close() 
page_soup=soup(page_html,"html.parser") 

images=page_soup.findAll("div",{"class":"lister-item-image float-left"}) 

containers=page_soup.findAll("div",{"class":"lister-item-content"}) 

filename="scifi.csv" 
f=open(filename,"w") 

headers="order, title, year"'\n' 

f.write(headers) 

for container in containers: 
    number=container.h3.findAll("span",{"class":"lister-item-index unbold text-primary"}) 
    order=number[0].text 

    atitle=container.h3.findAll("a") 
    title=atitle[0].text 

    date=container.h3.findAll("span",{"class":"lister-item-year text-muted unbold"}) 
    year=date[0].text 
    print("order:" + order) 
    print("title:" + title) 
    print("year:" + year) 
    f.write(order + "," +title+ "," +year + '\n') 

f.close() 
1

在for循環中添加您的f.write行。即添加縮進以匹配for循環體。