【问题标题】:Unable to search through python web scraper (URL doesn't change on search)无法通过 python 网络爬虫搜索(搜索时 URL 不会更改)
【发布时间】:2021-10-23 18:56:48
【问题描述】:

我无法通过网站上提供的搜索栏进行搜索。它采用地区的邮政编码并返回可用商店列表(相对)。

但与大多数网站不同,我正在开发的网站不会在我们输入字符串并从网站上提供的搜索框中搜索的事件中更改 URL。 我尝试了许多不同的技术,包括使用 selenium 和 Action Chains,但都是徒劳的。

请任何人看看你是否能找到方法。 我将不胜感激。

这是我尝试过的示例

# /// remaining code///

search_element = WebDriverWait(driver, 10).until(
    EC.presence_of_all_elements_located((By.ID, "partnerfinder-search"))
)

search_element = driver.find_element_by_id("partnerfinder-search")

action.send_keys(postal_code) # postal_code is just a character string
action.send_keys(Keys.ENTER)

# also tried these
action.send_keys("12")
# action.send_keys_to_element(search_element, "12")
# action.send_keys_to_element(search_element, Keys.ENTER)

# /// remaining code///

其他方法

# /// remaining code///

driver.get('URL')

search_element = driver.find_element_by_xpath("//input[@name='search']")

#also tried this
# search_element = driver.find_element_by_xpath("//form[@class='location-search__form']/input[@name='search']")

action.move_to_element(search_element)
action.send_keys(postal_code)
action.send_keys(Keys.ENTER)
action.perform()

# /// remaining code///

还尝试了许多其他解决方案,但似乎都不起作用。

【问题讨论】:

  • 你能给我测试的示例邮政编码吗?
  • 如果网址不变,还有什么要求,是不是阻止你做任何事情?
  • 我知道它对我来说也没有改变,我想知道你被卡住的原因是什么?
  • 当我尝试通过 python scraper 在搜索框中输入一些字符串时,它不起作用。意味着它不响应或提供任何反馈作为回报。只是停留在同一页面/表单上。因为它应该在响应中显示搜索结果。
  • 是的,没错。

标签: python selenium url web-scraping search


【解决方案1】:

这对我有用:

python

# remaining code

wait = WebDriverWait(driver, 50)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[class$='js-accept-all']"))).click()
wait.until(EC.element_to_be_clickable((By.ID, "partnerfinder-search"))).send_keys('01067', ' ')
driver.implicitly_wait(5)
driver.minimize_window()
driver.implicitly_wait(5)
driver.maximize_window()
wait.until(EC.element_to_be_clickable((By.XPATH, "//div[@class='pac-container pac-logo']/div[@class='pac-item']"))).click()

wait.until(EC.presence_of_all_elements_located((By.XPATH, "//div[@class='pf__item']")))

content = driver.page_source

# remaining code

感谢您的所有帮助@cruisepanday

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-03
    • 2017-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多