如何通过无头驱动程序访问网站,而不被拒绝权限

我试图使用无头镀铬驱动程序检索网站的
HTML代码.但是我得到了“许可被拒绝”的消息.如果我使用“常规”驱动程序,它一切正常.

有没有办法绕过那个?

这是我的第一篇文章,所以我为格式化中的任何潜在错误道歉

from selenium import webdriver

#Headless driver 

chrome_options = webdriver.ChromeOptions()

chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')                                             

driver1 = webdriver.Chrome(executable_path='./chromedriver', options=chrome_options, 
service_args=['--verbose', '--log-path=/tmp/chromedriver.log'])

driver1.get('https://www.size.co.uk/')
html = driver1.page_source
html

我收到的消息是:

<html xmlns="http://www.w3.org/1999/xhtml"><head>\n<title>Access Denied</title>\n</head><body>\n<h1>Access Denied</h1>\n \nYou don\'t have permission to access "http://www.size.co.uk/" on this server.<p>\nReference #18.ac81655f.1548818550.73b12da\n\n\n</p></body></html>

普通司机:

driver = webdriver.Chrome('./chromedriver')
driver.get('https://www.size.co.uk/')
html = driver.page_source
driver.quit()
html

理想情况下,我希望输出与后一种情况一样,不会每隔几秒钟弹出新窗口.

最佳答案 添加以下代码片段让我返回页面:

user_agent = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.50 Safari/537.36'    
chrome_options.add_argument('user-agent={0}'.format(user_agent))

该网站显然正在检查无头浏览器,然后拒绝他们访问.这是一篇关于避免检测的文章:Making Chrome Headless Undetectable

要使驱动程序使用用户代理,您可以运行以下命令:

driver.execute_script("return navigator.userAgent")

Chromes无头用户代理是这样的:

u’Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/71.0.3578.98 Safari/537.36′

点赞