我想從維基百科1992年至2014年的廣告牌前100名刮,然後清理數據。我在最後得到一個「無效文字」錯誤:瞭解無效的文字錯誤的網頁抓取
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'
任何人都知道這是爲什麼?
第1列中的數據不包含排名,它包含樂隊/歌手。但看看這個頁面,似乎並不是這樣。也許在頁面上有多個表格,而你正在弄錯了嗎? –