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

Using BeautifulSoup to find a attribute called data-stats

我目前正在开发一个网络抓取工具,它可以让我从一名足球运动员那里提取统计数据。通常,如果我可以抓取 div,这将是一项简单的任务,但是,该网站使用了一个名为 data-stats 的属性,并将其用作 class。这是一个例子。

<th scope="row" class="left " data-stat="year_id"><a href="/years/2000/">2000</a></th>

如果您想亲自检查网站,请点击此处 link。

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

我尝试了几种不同的方法。要么它根本不起作用,要么我将能够启动一个 for 循环并开始将内容放入数组中,但是您会注意到 table 中并非所有内容都是相同的 var 类型。

抱歉格式和语法问题。

这是我目前所拥有的,我敢肯定它不是最好看的代码,它主要只是我自己尝试过的代码以及一些在 Google 上搜索时混入的代码。忽略随机导入我正在尝试不同的东西

# import libraries
import csv
from datetime import datetime
import requests
from bs4 import BeautifulSoup
import lxml.html as lh
import pandas as pd

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

# request html
page = requests.get(url)

# Parse html using BeautifulSoup, you can use a different parser like lxml if present
soup = BeautifulSoup(page.content, 'lxml')
# find searches the given tag (div) with given class attribute and returns the first match it finds



headers = [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)

不太清楚你到底想提取什么,但这可能对你有点帮助:

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)

希望对您有所帮助!

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

这是从 table:

中获取您想要的所有数据的代码片段
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 而不是打印它