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

使用 BeautifulSoup 查找名为 data-stats 的属性

使用 BeautifulSoup 查找名为 data-stats 的属性

PIPIONE 2021-11-09 17:04:05
我目前正在开发一个网络爬虫,它可以让我从足球运动员那里提取统计数据。通常,如果我可以抓取 div,这将是一项简单的任务,但是,该网站使用名为 data-stats 的属性并将其用作类。这是一个例子。<th scope="row" class="left " data-stat="year_id"><a href="/years/2000/">2000</a></th>如果您想亲自查看该网站,请点击此处的链接。https://www.pro-football-reference.com/players/B/BradTo00.htm我尝试了几种不同的方法。要么它根本不起作用,要么我将能够启动一个 for 循环并开始将内容放入数组中,但是您会注意到并非表中的所有内容都是相同的 var 类型。对不起格式和语法。这是我到目前为止所拥有的,我确定它不是最好看的代码,它主要是我自己尝试过的代码以及在 Google 上搜索时混合的一些东西。忽略我正在尝试不同的东西的随机导入# import librariesimport csvfrom datetime import datetimeimport requestsfrom bs4 import BeautifulSoupimport lxml.html as lhimport pandas as pd# specify urlurl = 'https://www.pro-football-reference.com/players/B/BradTo00.htm'# request htmlpage = requests.get(url)# Parse html using BeautifulSoup, you can use a different parser like lxml if presentsoup = BeautifulSoup(page.content, 'lxml')# find searches the given tag (div) with given class attribute and returns the first match it findsheaders = [c.get_text() for c in soup.find(class_ = 'table_container').find_all('td')[0:31]]data = [[cell.get_text(strip=True) for cell in row.find_all('td')[0:32]]        for row in soup.find_all("tr", class_=True)]tags = soup.find(data ='pos')#stats = tags.find_all('td')print(tags)
查看完整描述

2 回答

?
慕妹3146593

TA贡献1820条经验 获得超9个赞

您需要使用getBeautifulSoup 中的方法按名称获取属性参见:BeautifulSoup Get Attribute


这是从表中获取您想要的所有数据的片段:


from bs4 import BeautifulSoup

import requests


url = "https://www.pro-football-reference.com/players/B/BradTo00.htm"

r = requests.get(url)

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


# Get table

table = soup.find(class_="table_outer_container")


# Get head

thead = table.find('thead')

th_head = thead.find_all('th')


for thh in th_head:

    # Get case value

    print(thh.get_text())


    # Get data-stat value

    print(thh.get('data-stat'))


# Get body

tbody = table.find('tbody')

tr_body = tbody.find_all('tr')


for trb in tr_body:

    # Get id

    print(trb.get('id'))


    # Get th data

    th = trb.find('th')

    print(th.get_text())

    print(th.get('data-stat'))


    for td in trb.find_all('td'):

        # Get case value

        print(td.get_text())

        # Get data-stat value

        print(td.get('data-stat'))


# Get footer

tfoot = table.find('tfoot')

thf = tfoot.find('th')


# Get case value

print(thf.get_text())

# Get data-stat value

print(thf.get('data-stat'))


for tdf in tfoot.find_all('td'):

    # Get case value

    print(tdf.get_text())

    # Get data-stat value

    print(tdf.get('data-stat'))

您当然可以将数据保存在 csv 甚至 json 中,而不是打印它


查看完整回答
反对 回复 2021-11-09
?
繁星coding

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

目前还不清楚您究竟要提取什么,但这可能会对您有所帮助:


import requests

from bs4 import BeautifulSoup as bs


url = 'https://www.pro-football-reference.com/players/B/BradTo00.htm'


page = requests.get(url)

soup = bs(page.text, "html.parser")


# Extract table

table = soup.find_all('table')

# Let's extract data from each row in table

for row in table:

    col = row.find_all('td')

    for c in col:

        print(c.text)

希望这可以帮助!


查看完整回答
反对 回复 2021-11-09
  • 2 回答
  • 0 关注
  • 281 浏览
慕课专栏
更多

添加回答

举报

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