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

关于#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日
  • 展开全部