我正试图从维基百科上搜集1992年至2014年的广告牌百强,然后清理数据。我在结尾处得到一个“无效文字”错误:
years = range(1992,2015)
yearstext = dict()
for year in years:
t_1992=requests.get('http://en.wikipedia.org/wiki/Billboard_Year-End_Hot_100_singles_of_%(year)s' % {"year":year})
soup = BeautifulSoup(t_1992.text, "html.parser")
yearstext[year]=soup
def parse_year(year, ytextdixt):
rows = soup.find("table", attrs={"class": "wikitable"}).find_all("tr")[1:]
cleaner = lambda r: [r[0].get_text(), int(r[1].get_text()), r[2].get_text(), r[2].find("a").get("href"), r[3].get_text(),r[3].find("a").get("href")]
fields = ["band_singer", "ranking", "song", "songurl","titletext","url"]
songs = [dict(zip(fields, cleaner(row.find_all("td")))) for row in rows]
ValueError: invalid literal for int() with base 10: 'Pharrell Williams'
有人知道这是为什么吗?你知道吗
做了一个小实验我发现:
提供:
所以使用:
提供:
这会让你走上正轨。你知道吗
'r[1].get\u text()'在某些情况下返回'Pharrell Williams'
然后“int(r[1].get\u text())”触发了此异常。你知道吗
所以重新检查你从网址上得到的细节。你知道吗
相关问题 更多 >
编程相关推荐