簡體   English   中英

使用Python / BS4刮取表格

[英]Scraping Table With Python/BS4

我正在嘗試使用BS4和Python 2.7從http://www.pro-football-reference.com/boxscores/201602070den.htm抓取“ Team Stats”表。 但是我無法接近任何地方,

url = 'http://www.pro-football-reference.com/boxscores/201602070den.htm'
page = requests.get(url)
soup = BeautifulSoup(page.text, "html5lib")
table=soup.findAll('table', {'id':"team_stats", "class":"stats_table"})  
print table

我以為上面的代碼可以工作,但是沒有運氣。

在這種情況下,問題是“ Team Stats”表位於您通過requests下載的HTML源代碼中的注釋內 找到注釋,並使用BeautifulSoup將其重新解析為“湯”對象:

import requests
from bs4 import BeautifulSoup, NavigableString

url = 'http://www.pro-football-reference.com/boxscores/201602070den.htm'
page = requests.get(url, headers={'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36'})

soup = BeautifulSoup(page.content, "html5lib")
comment = soup.find(text=lambda x: isinstance(x, NavigableString) and "team_stats" in x)

soup = BeautifulSoup(comment, "html5lib")
table = soup.find("table", id="team_stats")
print(table)

和/或,您可以將表加載到例如pandas數據幀中 ,使用起來非常方便:

import pandas as pd
import requests
from bs4 import BeautifulSoup
from bs4 import NavigableString

url = 'http://www.pro-football-reference.com/boxscores/201602070den.htm'
page = requests.get(url, headers={'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36'})

soup = BeautifulSoup(page.content, "html5lib")
comment = soup.find(text=lambda x: isinstance(x, NavigableString) and "team_stats" in x)

df = pd.read_html(comment)[0]
print(df)

印刷品:

            Unnamed: 0            DEN            CAR
0          First Downs             11             21
1         Rush-Yds-TDs        28-90-1       27-118-1
2    Cmp-Att-Yd-TD-INT  13-23-141-0-1  18-41-265-0-1
3         Sacked-Yards           5-37           7-68
4       Net Pass Yards            104            197
5          Total Yards            194            315
6         Fumbles-Lost            3-1            4-3
7            Turnovers              2              4
8      Penalties-Yards           6-51         12-102
9     Third Down Conv.           1-14           3-15
10   Fourth Down Conv.            0-0            0-0
11  Time of Possession          27:13          32:47

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM