【问题标题】:Using BeautifulSoup to find a attribute called data-stats使用 BeautifulSoup 查找名为 data-stats 的属性
【发布时间】:2019-02-15 03:19:43
【问题描述】:

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

【问题讨论】:

  • 你到底是桌子还是特定的桌子?

标签: python web-scraping beautifulsoup html-parsing


【解决方案1】:

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

这是一个sn-p,可以从表中获取你想要的所有数据:

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 中,而不是打印出来

【讨论】:

  • 这正是我想要找到的。 get() 可以找到名称晦涩的属性吗?我假设这就是它的工作原理
  • 是的,没错。如果这对您有好处,请接受答案并提高它;-)
【解决方案2】:

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

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)

希望这会有所帮助!

【讨论】:

  • 好的,抱歉解释不好。我要整张桌子。包括标题和年份。然后我希望最终能够将该表写入 csv 文件。您的代码版本显然效果最好,但我仍然不确定如何获取标题或年份。
猜你喜欢
  • 2019-04-21
  • 2022-07-18
  • 1970-01-01
  • 1970-01-01
  • 2022-11-27
  • 1970-01-01
  • 2020-03-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多