簡體   English   中英

BS4和抓取更新表

[英]BS4 and scraping an updating table

我正在嘗試從whoscored.com(下面的變量根目錄中的鏈接)抓取到所有EPL播放器的鏈接,這是代碼:

from bs4 import BeautifulSoup
from selenium import webdriver
root = "https://www.whoscored.com/Regions/252/Tournaments/2/Seasons/6335/Stages/13796/PlayerStatistics/England-Premier-League-2016-2017"
driver = webdriver.PhantomJS()
driver.get(root)
page = driver.page_source
soup = BeautifulSoup(page, "html.parser")
players = soup.find("div", {'id':'statistics-table-summary'})

print(players)

如果進入該頁面,您將看到一個玩家列表和一個下一步按鈕,以顯示接下來的10個玩家(其中29頁中有284個)我想要的輸出:保存指向每個十個玩家配置文件的鏈接,然后轉到下一個頁面,接下來的十個玩家直到完成

為此,我以為我會soup.find_all('a',{'class':'player-link})因為播放器的鏈接和名稱都在這樣的容器中,但此操作不會返回任何內容。 所以我想我會首先找到所有在那里的桌子,但這也沒有返回。 對此有何看法? 先感謝您

在獲取.page_source之前,您需要等待表被加載:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# ...

driver.get(root)

# wait for at least one player to be present in the statistics table
wait = WebDriverWait(driver, 10)
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "#statistics-table-summary .player-link")))

page = driver.page_source
driver.close()

# ...

暫無
暫無

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

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