我使用 Chrome-Headless 的原因是因為它就像一個真正的瀏覽器,但是當我將 chrome-headless Selenium 指向這個 Barnes and Noble 鏈接時:
https://www.barnesandnoble.com/w/the-woman-they-could-not-silence-kate-moore/1138489968?ean=9781728242576
我得到了這個回應,沒有導航到任何其他頁面
<html><head>
<title>Access Denied</title>
</head><body>
<h1>Access Denied</h1>
You don't have permission to access "https://www.barnesandnoble.com/w/the-woman-they-could-not-silence-kate-moore/1138489968?ean=9781728242576" on this server.<p>
Reference #
</p></body></html>
我知道我需要添加標頭和所有內容,但這與常規 GET 請求 標頭有何不同?
還有什么特別是讓 Chrome-headless 讓 Barnes 和 Nobel 離開?
我究竟做錯了什么?
我錯過了什么?
uj5u.com熱心網友回復:
您看到以下拒絕訪問錯誤頁面:

由于關鍵字Headless的存在用戶代理
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/98.0.4758.102 Safari/537.36
解決方案
您可以使用其他一些常規UserAgent覆寫默認用戶代理,如下所示:
代碼塊:
options = Options() options.headless = True options.add_argument("start-maximized") options.add_experimental_option("excludeSwitches", ["enable-automation"]) options.add_experimental_option('useAutomationExtension', False) options.add_argument('--disable-blink-features=AutomationControlled') options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36") s = Service('C:\\BrowserDrivers\\chromedriver.exe') driver = webdriver.Chrome(service=s, options=options) driver.get("https://www.barnesandnoble.com/w/the-woman-they-could-not-silence-kate-moore/1138489968?ean=9781728242576") driver.save_screenshot("barnesandnoble.png")截屏:

參考
您可以在以下位置找到一些相關的詳細討論:
- 如何使用 Selenium 和 Python 為用戶代理設定自定義名稱
- How to change the displayed operating system information within the useragent using Selenium and Python?
- How to change the Google Chrome UserAgent using the ChromeDriver installed through webdriver_manager
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/434330.html
標籤:硒 硒网络驱动程序 硒铬驱动程序 谷歌浏览器无头 拒绝访问
