【问题标题】:How to Retrieve all links from a dynamic website with selenium python如何使用 selenium python 从动态网站中检索所有链接
【发布时间】:2019-07-27 17:05:04
【问题描述】:

我有以下代码:

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


chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')

prefs = {'profile.managed_default_content_settings.images':2}
chrome_options.add_experimental_option("prefs", prefs)
driver = webdriver.Chrome(chrome_options=chrome_options) 
driver.get("http://biggestbook.com/ui/catalog.html#/search?cr=1&rs=12&st=BM&category=1")
wait = WebDriverWait(driver,20)
links = wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, ".ess-product-brand + [href]")))
results = [link.get_attribute("href") for link in links]
#print(links)
print(results)
driver.quit()

但是,我只获得特色产品的结果/链接,而不是所有产品。有时,(很少)如果我运行 20 次,我会得到所有的产品。但我希望始终能够获得所有产品。我还尝试了以下不同的方法:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
driver = webdriver.Chrome(chrome_options=chrome_options) 
driver.get("http://biggestbook.com/ui/catalog.html#/search?cr=1&rs=12&st=BM&category=1")

links = [elem.get_attribute("href") for elem in driver.find_elements_by_tag_name('a')]

print(links)

同样的问题。 我的问题是,我无法获得所有链接的原因是什么?这已经让我发疯了 2 周。我还试图延迟计时器,认为它可能没有加载,但它仍然没有工作。谢谢

【问题讨论】:

  • 所有产品有哪些?你在看Kitchen Roll Towels, Perforated, 2-Ply, 11 x 8, White, 85 Sheets/Roll, 30 Rls/CtPathways Soak-Proof Shield Mediumweight Paper Plates, 8 1/2", Grn/Burg, 125/Pk等吗?
  • 是的,没错。这些是测试用例。然而,我大多只得到精选的。

标签: javascript python json selenium-webdriver web-scraping


【解决方案1】:

您可以尝试通过提取结果总数并将特色总数添加到其中来尝试使用控制总数。这些数字已经可供您使用,因此您可以循环直到#hrefs 满足此要求。您可能想在循环中添加超时。

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

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')

prefs = {'profile.managed_default_content_settings.images':2}
chrome_options.add_experimental_option("prefs", prefs)
driver = webdriver.Chrome(chrome_options=chrome_options) 
driver.get("http://biggestbook.com/ui/catalog.html#/search?cr=1&rs=12&st=BM&category=1")
wait = WebDriverWait(driver,20)
nonFeaturedTotal = int(wait.until(EC.presence_of_element_located((By.CSS_SELECTOR , '.ess-view-item-count-text'))).text.split(' ')[-1])
featuredTotal = len(wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, ".ess-product-container-featured"))))
expectedTotal = featuredTotal + nonFeaturedTotal

while False:
    len(driver.find_elements_by_css_selector(".ess-product-brand + [href]")) == expectedTotal

links = driver.find_elements_by_css_selector(".ess-product-brand + [href]")
results = [link.get_attribute("href") for link in links]

print(len(results))
print(links)

driver.quit()

【讨论】:

  • 谢谢伙计。似乎有效,但有时它仍然只提供特色。尽管如此,我还是花时间理解这一点。谢谢!
  • 今晚我再看看
  • 有没有办法在不关闭会话的情况下自动进行多次重试?
  • 你可以使用 try catch。我还没有研究 selenium 本身是否提供回退重试。如果可用,文档可能会对此进行详细说明。
  • 嘿,根据您在上面为链接更正的代码,我尝试为表格制作一个,但无法正常工作
猜你喜欢
  • 2019-08-07
  • 2012-07-21
  • 1970-01-01
  • 2021-07-21
  • 1970-01-01
  • 1970-01-01
  • 2015-02-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多