python - python - 无法解析来自站点的文本,但来自我的可以
问题描述
我在一个网站上苦苦挣扎,这是我的代码
import csv
from bs4 import BeautifulSoup
page = requests.get('https://fmdataba.com/20/p/220045/diego-barbosa',headers = {'User-agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36'})
soup = BeautifulSoup(page.text, 'html.parser')
physical = {}
technical = {}
table = soup.find_all('table',{'class':'table tab61'})
print(len(table))
trs = table[0].find_all('tr')
for tr in trs:
tds = tr.find_all("td")
if len (tds) ==2:
physical[tds[0].text] = tds[1].text
print(physical)
table = soup.find_all('table',{'class':'table tab61'})
trs = table[1].find_all('tr')
for tr in trs:
tds = tr.find_all("td")
if len (tds) ==2:
technical[tds[0].text] = tds[1].text
print(technical)
问题是我需要解析玩家的能力,但它每次都会给我这个错误
Traceback (most recent call last):
File "C:\Users\marco\Desktop\fmdata.py", line 17, in <module>
trs = table[1].find_all('tr')
IndexError: list index out of range
我在我的网站中获得了部分 html 文件的副本
如果我尝试对此进行尝试,它会起作用,但不适用于实际站点
解决方案
我查看了您提供的链接:
https://fmdataba.com/20/p/220045/diego-barbosa
对于它的当前实例,它不包含类“table tab61”的元素。
短语“tab61”甚至不在页面的 html 中。
您的索引超出范围是因为 html 中没有 tab61 类