关于#python#的问题:代码可以运行并爬取信息,但会报错“IndexError: list index out of range”

爬虫成功但报错


import requests
from bs4 import BeautifulSoup

headers = {
"user-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36"
}
url = '放评论区'
response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')

tr_list = soup.select('.detail-context tr')
print(tr_list)
f = open('./女排名单/2015女排名单.txt', 'w+', encoding='utf-8')
for tr in tr_list:
  td_list = tr.select('td')
  name = td_list[0].text.strip()
  birthtime=td_list[1].text.strip()
  height=td_list[2].text.strip()
  smash=td_list[4].text.strip()
  block=td_list[5].text.strip()
  position = td_list[6].text.strip()
  province=td_list[-1].text.strip()

代码可以运行并爬取信息,但会报错“IndexError: list index out of range”,不知道怎么处理

http://www.volleychina.org/chnwvt2015.html

不知道你这个问题是否已经解决, 如果还没有解决的话:

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^