我目前正在开发一个网络刮板,它可以让我从一个足球运动员那里获取数据。通常这是一个简单的任务,如果我可以只抓取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)
您需要使用BeautifulSoup中的
get
方法按名称获取属性 参见:BeautifulSoup Get Attribute以下是从表中获取所需的所有数据的片段:
当然,您可以将数据保存在csv甚至json中,而不是打印出来
目前还不清楚您到底想提取什么,但这可能会对您有所帮助:
希望这有帮助!在
相关问题 更多 >
编程相关推荐