[英]trying to make a csv file from scrape data from Python
I'm new to Python and any type of coding ...I hope this is not too easy question. 我不熟悉Python和任何类型的编码...我希望这不是一个简单的问题。
I'm trying to make a csv file from the scrape data from the web. 我正在尝试从网上的抓取数据中制作一个CSV文件。
AttributeError: 'Doctype' object has no attribute 'find_all' AttributeError:“ Doctype”对象没有属性“ find_all”
But this error wont go away! 但是这个错误不会消失!
here's the whole code 这是整个代码
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()
You first assign to scores: 您首先分配分数:
scores = soup.find_all("div")
which is fine, but you then should walk over those scores: 很好,但是您应该走遍那些分数:
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
Trying to iterate over the Doc (ie soup
) using: 尝试使用以下方法遍历Doc(即
soup
):
for scores in soup:
makes no sense. 没有意义。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.