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

标签 python web-scraping beautifulsoup html-parsing

我目前正在开发一个网络抓取工具,它可以让我从足球运动员那里获取统计数据。通常,如果我可以抓取 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 类型。

对格式和语法感到抱歉。

这是我到目前为止所拥有的,我确信它不是最好看的代码,它主要只是我自己尝试过的代码和一些在谷歌搜索中混合的东西。忽略随机导入我正在尝试不同的事情

# 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)

最佳答案

您需要使用 BeautifulSoup 的 get 方法按名称获取属性 请参阅: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 中,而不是打印它

关于python - 使用 BeautifulSoup 查找名为 data-stats 的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54702255/

相关文章:

python - 对象成员的 Cython 缓冲区声明

python - 如何使用多索引加速 pandas 的索引?

python - Cassandra 多处理无法 pickle _thread.lock 对象

python - 使用Scrapy爬取公共(public)FTP服务器

python - BeautifulSoup使用独特的CSS选择器

python - python中的多线程 - 阻止调用在后台运行

python - 如何在python中将字符串列表转换为字典

python - 有没有更 Pythonic 的方法来合并两个 HTML 标题行与 colspans?

python - BeautifulSoup 查找多个类别

python - 无法摆脱 BeautifulSoup 造成的导航问题