为了账号安全,请及时绑定邮箱和手机立即绑定

我无法从 bs4 对象中找到重复出现的元素

我无法从 bs4 对象中找到重复出现的元素

桃花长相依 2023-12-04 17:04:03
我遇到的问题让我发疯。我正在尝试从职业足球参考网站中提取文本。我需要的信息位于网页第二部分中td显示的元素中。qb hurries该信息位于名为 的 td 元素中qb_hurry。这是我到目前为止所拥有的:res = requests.get('https://www.pro-football-reference.com/players/D/DonaAa00.htm')soup = bs4.BeautifulSoup(res.text, 'html.parser')我试过totalQbHurrys = soup.find('div', {'id':'all_detailed_defense'})当我解析美丽的汤对象并打印它时,我可以看到需要提取的信息。但是当我尝试检索td我需要的元素时totalQbHurrys = soup.find('div', {'id':'all_detailed_defense'}).find('td', {'data-stat':'qb_hurry'})它返回None,我认为我正在寻找的文本首先作为注释存在,但我无法获取我需要的实际 HTML 元素。有人知道成功定位该元素的方法吗qb_hurry?
查看完整描述

3 回答

?
FFIVE

TA贡献1797条经验 获得超6个赞

问题是该字段位于 HTML 注释标记内。


这是一个决议:


import bs4

import requests


res = requests.get('https://www.pro-football-reference.com/players/D/DonaAa00.htm')


soup = bs4.BeautifulSoup(res.text, 'html.parser')


extract = soup.find('div', {'id':'all_detailed_defense'})


for comments in extract.find_all(text=lambda text:isinstance(text, bs4.Comment)):

    comments.extract()


soup2 = bs4.BeautifulSoup(comments, 'html.parser')


totalQbHurrys = soup2.find('td', {'data-stat':'qb_hurry'})


print(totalQbHurrys)


查看完整回答
反对 回复 2023-12-04
?
德玛西亚99

TA贡献1770条经验 获得超3个赞

from selenium import webdriver

from selenium.webdriver.firefox.options import Options

import pandas as pd


options = Options()

options.add_argument('--headless')


driver = webdriver.Firefox(options=options)


driver.get("https://www.pro-football-reference.com/players/D/DonaAa00.htm")


df = pd.read_html(driver.page_source, attrs={

                  'class': 'row_summable sortable stats_table now_sortable'}, header=1)[0]


print(df.loc[1, 'Hrry'])


driver.quit()

输出:


32


查看完整回答
反对 回复 2023-12-04
?
红糖糍粑

TA贡献1815条经验 获得超6个赞

您需要的 HTML 位于注释内,因此在soup. 您需要首先获取注释,然后将其解析为新soup对象。然后您可以从中找到tr和th元素。例如:


from bs4 import BeautifulSoup, Comment

import requests


res = requests.get('https://www.pro-football-reference.com/players/D/DonaAa00.htm')

soup = BeautifulSoup(res.text, 'html.parser')

div = soup.find('div', {'id':'all_detailed_defense'})

comment_html = div.find(string=lambda text: isinstance(text, Comment))

comment_soup = BeautifulSoup(comment_html, 'html.parser')


for tr in comment_soup.find_all('tr'):

    row = [td.text for td in tr.find_all(['td', 'th'])]

    print(row)

给你:


['', 'Games', 'Pass Coverage', 'Pass Rush', 'Tackles']

['Year', 'Age', 'Tm', 'Pos', 'No.', 'G', 'GS', 'Int', 'Tgt', 'Cmp', 'Cmp%', 'Yds', 'Yds/Cmp', 'Yds/Tgt', 'TD', 'Rat', 'DADOT', 'Air', 'YAC', 'Bltz', 'Hrry', 'QBKD', 'Sk', 'Prss', 'Comb', 'MTkl', 'MTkl%']

['2018*+', '27', 'LAR', 'DT', '99', '16', '16', '0', '1', '0', '0.0%', '0', '', '0.0', '0', '39.6', '-2.0', '0', '0', '0', '30', '19', '20.5', '70', '59', '6', '9.2%']

['2019*+', '28', 'LAR', 'DT', '99', '16', '16', '0', '0', '0', '', '0', '', '', '0', '', '', '0', '0', '0', '32', '9', '12.5', '55', '48', '6', '11.1%']



查看完整回答
反对 回复 2023-12-04
  • 3 回答
  • 0 关注
  • 58 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信