Python网页抓取初学者:当使用BeautifulSoup并通过类查找时,我要么得到错误的类,要么得到“无”的结果,

icnyk63a  于 11个月前  发布在  Python
关注(0)|答案(1)|浏览(76)

我想从以下网站获取UCI积分:https://www.procyclingstats.com/rider/tadej-pogacar
首先,我只想从最近的比赛中获得uci积分。
到目前为止,我的代码是:

import requests
from bs4 import BeautifulSoup
import pandas as pd

page = requests.get("https://www.procyclingstats.com/rider/tadej-pogacar")
soup = BeautifulSoup(page.text, "lxml")
result_date = soup.find("td", class_ = False, id = False, style = False).text
print(result_date)
uci = soup.find("td", class_ = "cu600 ")
print(uci)

字符串
当我运行代码时,我要么得到类“gc cu600”,要么得到“None”,这取决于我是否搜索“cu600”或“cu600“。
有人知道我哪里做错了吗?

6ioyuze2

6ioyuze21#

以下是如何获得UCI Points

import requests
import pandas as pd

url = "https://www.procyclingstats.com/rider/tadej-pogacar"

df = pd.concat(pd.read_html(requests.get(url).text, flavor="lxml"))
print(df["Points UCI"].dropna().to_string(index=False))

字符串
要从最近的比赛中获得积分,只需将最后一行更改为:

print(df["Points UCI"].iloc[0])


输出(所有点):

800
    85
   150
   125
    60
     3
   600
  1040
   210
   150
   150
   110
    20
    15
    30
   210
    40
   110
   110
   100
    50
   400
   500
   800
   260
   440
   500
    60
60 +10
   +10
60 +10
  2.86
   200
 20 +5
    +5
 20 +5
 20 +5
   125

相关问题