我正在嘗試抓取網站
有一個開關顯示“當表被排序時,隱藏非限定符”。當它被切換時,輸出應該看起來更像[[Lamar Jackson, BAL, 24, qb, 7, 7, 76, 480, 2, 29, 31, 6.3, 68.6, 5], [Jalen Hurts, PHI, 23, qb 8, 8, 73, 432, 5, 29, 27, 5.9, 54.0, 5]...]. 但是當它沒有切換時,它看起來像我上面發布的輸出。
但是,當您抓取網站時,它默認為關閉。有沒有辦法打開這個?
uj5u.com熱心網友回復:
您可以按 id 定位表,然后排除tds具有non_qual. 我會使用這些行中的 html,用table標簽包裹,用 Pandas 重建表格。最后,整理桌子。
鑒于似乎有一些聯系,在 內Y/A,看起來在Attdesc上有一個二級排序,例如頁面輸出 Y/A 4.5 的以下順序(當前 2021-11-07)
18 Aaron Jones
19 Dalvin Cook
20 Melvin Gordon
21 David Montgomery
代碼:
import pandas as pd
from bs4 import BeautifulSoup as bs
import requests
r = requests.get(
'https://www.pro-football-reference.com/years/2021/rushing.htm')
soup = bs(r.content, 'lxml')
t = pd.read_html('<table>' ''.join([str(r) for r in soup.select(
'#rushing tr:not(:has(td.non_qual))')]) '</table>')[0]
t.columns = [i[1] for i in t.columns]
t = t[t.Rk != 'Rk'].apply(pd.to_numeric, errors="ignore")
t.sort_values(['Y/A', 'Att'], ascending=[False, False], inplace=True)
t.Rk = [i 1 for i in range(len(t.index))]
t.reset_index(drop=True, inplace=True)
t
示例輸出:

轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/354331.html
上一篇:如何抓取沒有不同類的表資料?
下一篇:無法使用python分頁抓取網頁
