使用BeautifulSoup查找名为datastats的属性

2024-04-19 01:07:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在开发一个网络刮板,它可以让我从一个足球运动员那里获取数据。通常这是一个简单的任务,如果我可以只抓取div然而,这个网站使用了一个名为datastats的属性并像类一样使用它。这就是一个例子。在

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

Tags: inimporturlfordatagethtmlall
2条回答

您需要使用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中,而不是打印出来

目前还不清楚您到底想提取什么,但这可能会对您有所帮助:

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)

希望这有帮助!在

相关问题 更多 >