【问题标题】:find values from drop down in single listings with beautiful soup从带有漂亮汤的单个列表中的下拉列表中查找值
【发布时间】:2021-04-13 23:24:59
【问题描述】:

我是网络抓取的新手,并且有一个带有下拉菜单的汽车网站。如何在下拉菜单中选择汽车品牌(例如 BMW),然后点击查看所有具有该品牌的汽车? Beautifulsoup 甚至可以做到这一点,还是我需要 Selenium

网站是这个https://www.leasingmarkt.ch/listing。我设法获得了每个汽车品牌需要输入的data_values(数字),但我不知道如何将它们输入到网站然后点击?

for x in soup.find_all("div", class_ = "dropdown manufacturer-dropdown"):
    for car in x.find_all('li'):  
        try:
            cars_id[car.find("span").text] = car.get('data-value')
        except AttributeError:
            continue

我认为只需更改 url 以使汽车品牌的编号出现在链接中即可。但是如何选择然后单击(如果可能的话,使用BeautifulSoup)?

【问题讨论】:

  • 如果你需要点击然后使用 Selenium 的函数而不是 Beautifulsoup。您不能“点击”Beautifulsoup - 您只能使用requests/urllib 获取 URL 并加载新的 HTML。但是如果页面使用 JavaScript 加载新页面,那么您将需要 Selenium 而没有 Beautifulsoup
  • 此页面使用 JavaScript 更新页面。当您选择元素时,它会运行 JavaScritp,它运行像 https://www.leasingmarkt.ch/lmrpc/listing-ajax-update?lmrpc=1.0&query=%22v%3D2%26ids%3D%26sort%3Dpopularity%26takeoverContract%3D0%26newContract%3D1%26manufacturer%3D13 ... 这样的 url 并以 JSON 格式获取结果并用 HTML 替换它。在此 url 中,您可以看到manufacturer .. 13,其中13BMW 下拉列表中的值。您可以将此 URL 与 requests 一起使用来获取页面,但使用 Selenium 会更容易。
  • 你到底是怎么得到这个网址的?
  • 我在Firefox/Chrome 中使用了DevTools(标签Network,过滤器XHR)来查看所有通过JavaScript 从浏览器发送到服务器的请求。
  • 我的意思是获取上述网址的代码?它可以与 Selenium 一起使用吗?

标签: python selenium web-scraping beautifulsoup


【解决方案1】:

如果你使用 Selenium,应该很简单:

driver.implicitly_wait(20)  # Add this at least, if it won't work add explicit wait
dropdown = driver.find_element_by_id("manufacturer")
dropdown.click()
input_field = driver.find_element_by_css_selector(".select.searchable.btn.manufacturer-dropdown>.selection").send_keys("bmw")

使用等待https://selenium-python.readthedocs.io/waits.html

如果隐式等待不适用于这种情况,请使用显式等待:

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

wait = WebDriverWait(driver, timeout=30)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#manufacturer")))
dropdown = driver.find_element_by_id("manufacturer")
dropdown.click()
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".select.searchable.btn.manufacturer-dropdown>.selection")))
input_field = driver.find_element_by_css_selector(".select.searchable.btn.manufacturer-dropdown>.selection")
input_field.clear()
input_field.send_keys("bmw")

尝试 2: 对于制造商领域,请尝试几个定位器:#manufacturer> .select>.selection#manufacturer> .select

如果给定的字段不起作用,也可以尝试使用#manufacturer> .select 作为输入字段。

另外,对于第一个元素,请尝试将 element_to_be_clickable 替换为 visibility_of_element_located

【讨论】:

  • 谢谢,但它给了我一个错误ElementNotInteractableException: Message: element not interactable。你知道为什么吗?
  • 在哪个定位器上?
  • 我仍然得到错误。 ElementNotInteractableException: Message: element not interactable (Session info: headless chrome=89.0.4389.114)。定位器是什么意思?
  • 如果 input_field.clear() 不起作用,请使用 input_field.click() 然后输入您的搜索文本
  • 非常感谢。现在wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#manufacturer")))TimeoutException: Message:
猜你喜欢
  • 2017-01-18
  • 2020-06-19
  • 1970-01-01
  • 2023-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-20
  • 2021-07-15
相关资源
最近更新 更多