Qwerthd 2023-03-08 22:03 采纳率: 71.1%
浏览 27
已结题

关于#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”,不知道怎么处理

  • 写回答

2条回答 默认 最新

  • Qwerthd 2023-03-08 22:07
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 4月28日
  • 已采纳回答 4月20日
  • 修改了问题 3月8日
  • 修改了问题 3月8日
  • 展开全部

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题