Python 硒罐';不要翻开第二页

Python 硒罐';不要翻开第二页,python,selenium,google-chrome,selenium-webdriver,automation,Python,Selenium,Google Chrome,Selenium Webdriver,Automation,我正在使用Selenium打开站点的不同页面。已尝试多次,但浏览器在首次GET呼叫后未打开第二个网页。已经在Chrome和Safari上试用过。这是我的密码: driver = webdriver.Chrome() driver.get("https://www.justdial.com/Chennai/Hr-Consultancy-Services/nct-10258625/page-2") driver.set_page_load_timeout(30) driver.g

我正在使用Selenium打开站点的不同页面。已尝试多次,但浏览器在首次GET呼叫后未打开第二个网页。已经在Chrome和Safari上试用过。这是我的密码:

driver = webdriver.Chrome()
driver.get("https://www.justdial.com/Chennai/Hr-Consultancy-Services/nct-10258625/page-2")
driver.set_page_load_timeout(30)
driver.get("https://www.justdial.com/Chennai/Hr-Consultancy-Services/nct-10258625/page-3")
下面是我在第二次通话中遇到的错误:

来自网络日志的信息是错误504,但我已经验证,在浏览器的另一个窗口上执行时,它工作正常,没有自动化

options.add_experimental_option(
    "excludeSwitches", ['enable-automation'])

options.add_argument(
    "user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36")
options.add_argument("--remote-debugging-port=9222")

driver = webdriver.Chrome(options=options)
driver.get(
    "https://www.justdial.com/Chennai/Hr-Consultancy-Services/nct-10258625/page-2")
driver.set_page_load_timeout(30)
driver.get(
    "https://www.justdial.com/Chennai/Hr-Consultancy-Services/nct-10258625/page-3")
该网站正在检测自动化使用上述代码:)

您也可以在单行中执行此操作

只需添加以下参数:

options.add_argument('--disable-blink-features=AutomationControlled')

禁用enable automation(启用自动化)或禁用automation controller(禁用自动化控制器)将禁用webdriver.navigator(很少有网站使用它来检测自动化脚本)

有关您的用例的更多信息将有助于构建更规范的答案。但是,我能够使用最小化的代码块访问,如下所示:

  • 代码块:

    from selenium import webdriver
    
    options = webdriver.ChromeOptions() 
    options.add_argument("start-maximized")
    driver = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
    driver.get("https://www.justdial.com/Chennai/Hr-Consultancy-Services/nct-10258625/page-2")
    
  • 浏览器快照:

但是在一个接一个地发送多个
get()
时:

driver.get("https://www.justdial.com/Chennai/Hr-Consultancy-Services/nct-10258625/page-2")
driver.get("https://www.justdial.com/Chennai/Hr-Consultancy-Services/nct-10258625/page-3")
似乎是这样,显示了以下错误:

An error occurred while processing your request.
Reference #97.e5732c31.1612205693.6fd2708

解决方案 要避免检测,可以添加以下选项:

--disable-blink-features=AutomationControlled

例子
看起来页面确实打开了,只是打开时Web服务器出现错误。是的,这是一个服务器错误,但没有更多的线索来帮助解决相同的问题。对于其他2个站点也一样,我想知道URL操作是否比简单的GET提供的内容更多。您确定这是正确且完整的URL吗?你能手动执行吗?当手动运行时,浏览器中的“网络”选项卡显示发生了什么?代码对我来说运行得非常好。我也使用了Chrome。@AjayKumar请点击勾号接受,并请向上投票:)
from selenium import webdriver

options = webdriver.ChromeOptions() 
options.add_argument("start-maximized")
options.add_argument('--disable-blink-features=AutomationControlled')
driver = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
driver.get("https://www.justdial.com/Chennai/Hr-Consultancy-Services/nct-10258625/page-2")
driver.get("https://www.justdial.com/Chennai/Hr-Consultancy-Services/nct-10258625/page-3")