2017-06-18 64 views
0

我是新來的Python和任何類型的編碼...我希望這不是太容易的問題。試圖製作一個csv文件從Python中抓取數據

我想從網上的刮刮數據做一個csv文件。

AttributeError的: 'DOCTYPE' 對象有沒有屬性 'find_all'

但這個錯誤不會消失!

這裏是整個代碼

import bs4 as bs 
import urllib.request 


req = urllib.request.Request('http://www.mobygames.com/game/tom-clancys-rainbow-six-siege',headers={'User-Agent': 'Mozilla/5.0'}) 

sauce = urllib.request.urlopen(req).read() 

soup = bs.BeautifulSoup(sauce,'lxml') 

scores = soup.find_all("div") 

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

headers = "Hi, Med, Low\n" 

f.write(headers) 

for scores in soup: 
    scoreHi = scores.find_all("div", {"class":"scoreHi"}) 
    Hi = scoreHi[0].text 
    scoreMed = scores.find_all("div", {"class":"scoreMed"}) 
    Med = scoreMed[0].text 
    scoreLow = scores.find_all("div", {"class":"scoreLow"}) 
    Low = scoreLow[0].text 

    print ("Hi: " + Hi) 

    print ("Med: " + Med) 

    print ("Low: "+ Low) 

    f.write(Hi + "," + Med.replace(",","|") + "," + Low + "\n") 


f.close() 

回答

0

你先分配給的分數:

scores = soup.find_all("div") 

這是很好的,但你應該走過去那些成績:

for score in scores: 
    scoreHi = score.find_all("div", {"class":"scoreHi"}) 
    Hi = scoreHi[0].text 
    scoreMed = score.find_all("div", {"class":"scoreMed"}) 
    Med = scoreMed[0].text 
    scoreLow = score.find_all("div", {"class":"scoreLow"}) 
    Low = scoreLow[0].text 

嘗試迭代Doc(即soup)使用:

for scores in soup: 

是沒有意義的。

+0

謝謝!我不知道我是如何錯過的! –

+0

如果此答案解決了您的問題,請通過單擊答案旁邊的✔(複選標記)來考慮*接受*。這是其他人知道你的問題已經解決的方式,沒有閱讀評論。它還會在列表中更改問題的外觀和此答案。如果有更好的答案出現,您可以隨時更改接受的答案。 – Anthon

相關問題