簡體   English   中英

試圖從網站上抓取一張桌子<div tags< div><div id="text_translate"><p> 我正在嘗試刮這張桌子<a href="https://momentranks.com/topshot/account/mariodustice?limit=250" rel="nofollow noreferrer">https://momentranks.com/topshot/account/mariodustice?limit=250</a></p><p> 我試過這個:</p><pre> import requests from bs4 import BeautifulSoup url = 'https://momentranks.com/topshot/account/mariodustice?limit=250' page = requests.get(url) soup = BeautifulSoup(page.content, 'lxml') table = soup.find_all('table', attrs={'class':'Table_tr__1JI4P'})</pre><p> 但它返回一個空列表。 有人可以就如何解決這個問題提供建議嗎?</p></div></div>

[英]Trying to scrape a table from a website with <div tags

我正在嘗試刮這張桌子https://momentranks.com/topshot/account/mariodustice?limit=250

我試過這個:

import requests
from bs4 import BeautifulSoup
url = 'https://momentranks.com/topshot/account/mariodustice?limit=250'
page = requests.get(url)
soup = BeautifulSoup(page.content, 'lxml')
table = soup.find_all('table', attrs={'class':'Table_tr__1JI4P'})

但它返回一個空列表。 有人可以就如何解決這個問題提供建議嗎?

當有可用的 api 時,Selenium 有點矯枉過正。 直接獲取數據即可:

import requests
import pandas as pd

url = 'https://momentranks.com/api/account/details'

rows = []
page = 0
while True:
    
    payload = {
        'filters': {'page': '%s' %page, 'limit': "250", 'type': "moments"},
        'flowAddress': "f64f1763e61e4087"}
    
    jsonData = requests.post(url, json=payload).json()
    
    data = jsonData['data']
    rows += data
    
    print('%s of %s' %(len(rows),jsonData['totalCount'] ))
    if len(rows) == jsonData['totalCount']:
        break
    
    page += 1

df = pd.DataFrame(rows)

Output:

print(df)
                           _id    flowId  ...  challenges priceFloor
0     619d2f82fda908ecbe74b607  24001245  ...         NaN        NaN
1     61ba30837c1f070eadc0f8e4  25651781  ...         NaN        NaN
2     618d87b290209c5a51128516  21958292  ...         NaN        NaN
3     61aea763fda908ecbe9e8fbf  25201655  ...         NaN        NaN
4     60c38188e245f89daf7c4383  15153366  ...         NaN        NaN
                       ...       ...  ...         ...        ...
1787  61d0a2c37c1f070ead6b10a8  27014524  ...         NaN        NaN
1788  61d0a2c37c1f070ead6b10a8  27025557  ...         NaN        NaN
1789  61e9fafcd8acfcf57792dc5d  28711771  ...         NaN        NaN
1790  61ef40fcd8acfcf577273709  28723650  ...         NaN        NaN
1791  616a6dcb14bfee6c9aba30f9  18394076  ...         NaN        NaN

[1792 rows x 40 columns]

數據使用 js 代碼索引到頁面中,您不能單獨使用請求,但是您可以使用 selenium 請記住,Selenium 的 driver.get 不會等待頁面完成加載,這意味着您需要等待

在這里讓您開始使用 selenium

url = 'https://momentranks.com/topshot/account/mariodustice?limit=250'
page = driver.get(url)
time.sleep(5) #edit the time of this depending on your case (in seconds)
soup = BeautifulSoup(page.source, 'lxml')
table = soup.find_all('table', attrs={'class':'Table_tr__1JI4P'})

您在瀏覽器中看到的源 HTML 是使用 javascript 呈現的。 當您使用requests時,這不會發生,這就是您的腳本無法正常工作的原因。 如果您打印返回的 HTML,它將不包含您想要的信息。

所有信息都可以通過 API 獲得,您的瀏覽器會調用它來構建頁面。 您需要詳細查看返回的 JSON 數據結構,以決定您希望提取哪些信息。

以下示例顯示了如何獲取每個玩家的名稱和 MR 值的列表:

import requests
from bs4 import BeautifulSoup
import json

s = requests.Session()
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36'}
url = 'https://momentranks.com/topshot/account/mariodustice?limit=250'
req_main = s.get(url, headers=headers)
soup = BeautifulSoup(req_main.content, 'lxml')
data = soup.find('script', id='__NEXT_DATA__')
json_data = json.loads(data.string)
account = json_data['props']['pageProps']['account']['flowAddress']
post_data = {"flowAddress" : account,"filters" : {"page" : 0, "limit":"250", "type":"moments"}}
req_json = s.post('https://momentranks.com/api/account/details', headers=headers, data=post_data)
player_data = req_json.json()

for player in player_data['data']:
    name = player['moment']['playerName']
    mrvalue = player['MRvalue']
    print(f"{name:30} ${mrvalue:.02f}")

給你 output 開始:

Scottie Barnes                 $672.38
Cade Cunningham                $549.00
Josh Giddey                    $527.11
Franz Wagner                   $439.26
Trae Young                     $429.51
A'ja Wilson                    $387.07
Ja Morant                      $386.00

第一個頁面請求需要flowAddress以允許正確使用 API。 這恰好嵌入在 HTML 底部的<script>部分中。

所有這些都是通過使用瀏覽器的網絡工具來觀察實際網頁如何向服務器發出請求以構建其頁面來解決的。

暫無
暫無

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

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