簡體   English   中英

我無法使用 selenium 和美麗的湯來抓取表數據

[英]I'm not able to scrape table data using selenium and beautiful soup

我已經盡我所能 go 但我似乎無法從表中抓取數據。 我已經通過stackoverflow搜索了答案,但似乎沒有任何效果。 基本上表格是空的,或者我根本無法在表格中找到元素。 我正在使用來自雅虎每日幻想網頁的表格。

注意:當前使用的 web 地址可能會每周更改,因此將來可能不是有效地址。

當前代碼:

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

driver = webdriver.Chrome()
driver.get("https://sports.yahoo.com/dailyfantasy/contest/5416455/setlineup")

response = wait(driver, 10).until(EC.presence_of_element_located((By.TAG_NAME,"data-tst-player-id")))
driver.quit

soup = BeautifulSoup(response, 'lxml')
with open('test.txt','w', encoding='utf-8') as f_out:
    f_out.write(soup.prettify())

沒有您在該行中提供的類名或 id 的元素

response = wait(driver, 10).until(EC.presence_of_element_located((By.TAG_NAME,"data-tst-player-id")))

但是,有些標簽帶有“data-tst”屬性,因此您可以使用它來確保您的頁面已加載,並且在這一行

driver.quit

你什么都不做,你必須調用 function driver.quit()。 工作代碼:

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

driver = webdriver.Chrome()
driver.get("https://sports.yahoo.com/dailyfantasy/contest/5416455/setlineup")
wait(driver, 1).until(EC.presence_of_element_located((By.CSS_SELECTOR,"[data-tst]")))
response=driver.page_source
driver.quit()

soup = BeautifulSoup(response, 'lxml')
with open('test.txt','w', encoding='utf-8') as f_out:
    f_out.write(soup.prettify())

暫無
暫無

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

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