37,743
社区成员




soup = BeautifulSoup ( html, "lxml" )
trs = soup.find_all('tr')[1:]
movies = []
for tr in trs:
movie = {}
tds = tr.find_all("td")
title = tds[0].string
category = tds[1].string
nums = tds[2].string
city = tds[3].string
time = tds[4].string
movie['title'] = title
movie['category'] = category
movie['nums'] = nums
movie['city'] = city
movie['time'] = time
movies.append(movie)
print(movies)
Traceback (most recent call last):
File "E:/资源/python 爬虫/bs4_hj/123123.py", line 138, in <module>
category = tds[1].string
IndexError: list index out of range
soup = BeautifulSoup ( html, "lxml" )
trs = soup.find_all('tr')[1:]
print(trs) # 查看一下这里是否有数据
movies = []
for tr in trs:
movie = {}
tds = tr.find_all("td") # 你这里是 遍历trs,查找带有td的数据,如果没有的话使用tds[0] 获取下标会报错
if len(tds) > 0:
title = tds[0].string
category = tds[1].string
nums = tds[2].string
city = tds[3].string
time = tds[4].string
movie['title'] = title
movie['category'] = category
movie['nums'] = nums
movie['city'] = city
movie['time'] = time
movies.append(movie)
print(movies)