【问题标题】:Selenium python chrome webdriverSelenium python chrome webdriver
【发布时间】:2021-04-19 05:34:50
【问题描述】:

我正在尝试使用 selenium 从 mouser.com 网站上抓取一些数据,但在搜索栏中发送了一些数据之后。该网站导致访问被拒绝,我需要帮助绕过它。我尝试使用代理,但还是一样。

import time
from openpyxl import load_workbook
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from fake_useragent import UserAgent

driver = webdriver.Chrome(executable_path='C:/Users/amuri/AppData/Local/Microsoft/WindowsApps/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/site-packages/chromedriver.exe')
driver.implicitly_wait(1)

#def get_comp_type(comp_pn):
url ='https://www.mouser.com/'
driver.get(url)
print(driver.title)
wait = WebDriverWait(driver, timeout=10)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".form-control.headerSearchBox.search-input.js-search-autosuggest.as-input")))
elem = driver.find_element_by_css_selector(".form-control.headerSearchBox.search-input.js-search-autosuggest.as-input")
elem.click()
elem.send_keys("myString")
elem.send_keys(Keys.RETURN)
time.sleep(1)

【问题讨论】:

  • 不要在短时间内发送太多请求。许多网站认为它是一个机器人并阻止它。我可以建议的是在结果出来之后.. 你最好做一些活动,比如向下滚动和向上滚动.. 在那里等待一段时间,然后尝试新的 URL。

标签: python selenium selenium-webdriver webdriver


【解决方案1】:
from selenium.webdriver.chrome.options import Option
options = Options()
from fake_useragent import UserAgent
ua = UserAgent()
user_agent = ua.random
print(user_agent)
options.add_argument(f'user-agent={user_agent}')
driver = webdriver.Chrome(executable_path='C:/Users/amuri/AppData/Local/Microsoft/WindowsApps/PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0/site-packages/chromedriver.exe',options=options)

您没有在选项中的任何地方使用您的用户代理。此外,您还需要设置 javascript 和 cookie。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-27
    • 2017-07-17
    • 1970-01-01
    • 1970-01-01
    • 2017-05-18
    • 2018-02-18
    • 2019-11-15
    • 1970-01-01
    相关资源
    最近更新 更多