亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

需要使用 xpath 和 beautifulsoup 從網站抓取數據

需要使用 xpath 和 beautifulsoup 從網站抓取數據

寶慕林4294392 2023-12-20 10:23:41
大家好網站鏈接故事是他試圖抓取一個名為“Open Bets”的表,但不幸的是該表沒有類或 id,我使用 beautifulsoup 來抓取該表,并使用 XPath 來檢測該表,但沒有發生如圖所示的情況以下:我嘗試從表中抓取數據并檢測名為“Team A”與“Team B”的列,重點是我顯示了這樣的數據print(Player1," vs ",Player2)print("Odds ",odds)print("Rate ",rate)print("stake ",stake)我想你會明白我在這里試圖做什么,如下表:我嘗試聯系網站管理員向代碼源添加類或其他內容,但沒有任何結果。from lxml import htmlimport requestspage = requests.get('https://tipsters.asianbookie.com/index.cfm?player=Mitya68&ID=297754')tree = html.fromstring(page.content)ID = tree.xpath('/html/body/table[2]/tbody/tr/td[3]/table[7]')print(ID)這是我使用的代碼,如果有人可以幫忙那就太好了=)
查看完整描述

1 回答

?
翻過高山走不出你

TA貢獻1875條經驗 獲得超3個贊

一個簡單的方法是使用pandas. 操作方法如下:


import pandas as pd

import requests


r = requests.get('https://tipsters.asianbookie.com/index.cfm?player=Mitya68&ID=297754&sortleague=1#playersopenbets&tz=5.5').text


dfs = pd.read_html(r)


df = dfs[141]


df.columns = df.iloc[0]


df = df.drop(0)


df['Bet Placed ≡'] = [value.split('.')[-1] for value in df['Bet Placed ≡']]


print(df)

輸出:


0   Bet Placed ≡              Team A  ...   Rate         Pending Status

1    9 hours ago         Real Madrid  ...  1.975            pending ?-?

2    9 hours ago   Red Bull Salzburg  ...  1.875            pending ?-?

3    9 hours ago                Ajax  ...   2.00            pending ?-?

4    9 hours ago       Bayern Munich  ...   2.00            pending ?-?

5    9 hours ago       Bayern Munich  ...   1.85            pending ?-?

6    9 hours ago         Inter Milan  ...  1.875            pending ?-?

7    9 hours ago     Manchester City  ...   1.95            pending ?-?

8    9 hours ago         Midtjylland  ...  1.875            pending ?-?

9    9 hours ago  Olympiakos Piraeus  ...   1.95            pending ?-?

10   9 hours ago          Hamburg SV  ...  1.925            pending ?-?

11   9 hours ago         Vissel Kobe  ...  1.925   Lost(-25,000) FT 1-3

12   9 hours ago     Shonan Bellmare  ...  1.825   Won?(+10,313) FT 0-0

13   9 hours ago    Yokohama Marinos  ...  2.025   Won?(+12,812) FT 2-1

14   9 hours ago        RKC Waalwijk  ...  1.875            pending ?-?

15   9 hours ago            Espanyol  ...  2.075  lose(-25,000) 29' 1-0


[15 rows x 7 columns]

您還可以通過將這些行添加到代碼中來獲取這些值作為單獨的列表:


team_a = list(df['Team A'])

team_b = list(df['Team B'])

rate = list(df['Rate'])

stake = list(df['Stake'])

如果您想以您提到的格式打印它們,請將這些行添加到您的代碼中:


final_lst = zip(team_a,team_b,stake,rate)


for teamA,teamB,stakee,ratee in final_lst:

    print(f"{teamA} vs {teamB} - Stake: {stakee}, Rate: {ratee}")

輸出:


Real Madrid vs Shaktar Donetsk - Stake: 25000.00, Rate: 1.975

Red Bull Salzburg vs Lokomotiv Moscow - Stake: 100000.00, Rate: 1.875

Ajax vs Liverpool - Stake: 25000.00, Rate: 2.00

Bayern Munich vs Atl. Madrid - Stake: 25000.00, Rate: 2.00

Bayern Munich vs Atl. Madrid - Stake: 25000.00, Rate: 1.85

Inter Milan vs Monchengladbach - Stake: 25000.00, Rate: 1.875

Manchester City vs Porto - Stake: 25000.00, Rate: 1.95

Midtjylland vs Atalanta - Stake: 100000.00, Rate: 1.875

Olympiakos Piraeus vs Marseille - Stake: 25000.00, Rate: 1.95

Hamburg SV vs Erzgebirge Aue - Stake: 100000.00, Rate: 1.925

Vissel Kobe vs Kashima Antlers - Stake: 25000.00, Rate: 1.925

Shonan Bellmare vs Sagan Tosu - Stake: 25000.00, Rate: 1.825

Yokohama Marinos vs Nagoya - Stake: 25000.00, Rate: 2.025

RKC Waalwijk vs PEC Zwolle - Stake: 25000.00, Rate: 1.875

Espanyol vs Mirandes - Stake: 25000.00, Rate: 2.075


查看完整回答
反對 回復 2023-12-20
  • 1 回答
  • 0 關注
  • 212 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號