3 回答

TA貢獻(xiàn)1785條經(jīng)驗(yàn) 獲得超4個(gè)贊
你不需要硒的費(fèi)用。您可以對(duì)頁(yè)面執(zhí)行相同的 GET 請(qǐng)求,然后從返回的 json 中提取 html 并使用 bs4 解析并提取鏈接
import requests
from bs4 import BeautifulSoup as bs
r = requests.get('https://epl.bibliocommons.com/item/load_ugc_content/2300646980').json()
soup = bs(r['html'], 'lxml')
links = [i['href'] for i in soup.select('[data-test-id="staff-lists-that-include-this-title"] + div [href]')]
print(len(links))
print(links)

TA貢獻(xiàn)1808條經(jīng)驗(yàn) 獲得超4個(gè)贊
我已經(jīng)抓取了您的頁(yè)面并編寫了一個(gè) XPath,它將找到li“包含此職位的員工列表”下的所有元素。更新為包含wait所有相關(guān)li元素的a 。
WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.XPath, "//div[h4[text()='Staff Lists that include this Title']]/div[2]/ul/li[@class='']")))
driver.find_elements_by_xpath("//div[h4[text()='Staff Lists that include this Title']]/div[2]/ul/li[not(contains(@class, 'extra'))]")
此 XPath 查詢包含文本“包含此職位的員工列表”的元素下的div所有l(wèi)i項(xiàng)目的主元素h4。然后我們查詢div[2]哪些包含相關(guān)li項(xiàng)目。最后的查詢是針對(duì)li具有 EMPTY 類名的元素。從頁(yè)面源碼中可以看出,有很多隱藏的li帶有class='extra'屬性的元素。我們不想要這些li元素,因此我們繼續(xù)查詢not(contains(@class=, 'extra'))以獲取li沒(méi)有extra類名的元素。
如果上述 XPath 不起作用,我還修改了您在原始問(wèn)題中發(fā)布的另一個(gè) XPath:
WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.XPath, "//*[@id="rightBar"]/div[3]/div/div[2]/ul/li[not(contains(@class, 'extra'))]")))
driver.find_elements_by_xpath("//*[@id="rightBar"]/div[3]/div/div[2]/ul/li[not(contains(@class, 'extra'))]")
對(duì)于您提供的 URL,兩個(gè)查詢都檢索了 5 個(gè)結(jié)果:

TA貢獻(xiàn)1840條經(jīng)驗(yàn) 獲得超5個(gè)贊
獲取所有的anchor 標(biāo)簽下的Staff Lists that Include that TitleinduceWebDriverWait和 presence_of_all_elements_located() 這會(huì)給你100 個(gè)鏈接。
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=webdriver.Chrome()
driver.get("https://epl.bibliocommons.com/item/show/2300646980")
elements=WebDriverWait(driver,10).until(EC.presence_of_all_elements_located((By.XPATH,'//h4[@data-test-id="staff-lists-that-include-this-title"]/following::div[1]//li/a')))
print(len(elements))
for ele in elements:
print(ele.get_attribute('href'))
添加回答
舉報(bào)