Selenium用户代理在Heroku上不起作用

rjee0c15  于 2022-11-13  发布在  其他
关注(0)|答案(2)|浏览(142)

我试图访问网站,但在无头模式下,我得到这个:

<html><head>
<title>Access Denied</title>
</head><body>
<h1>Access Denied</h1>
 
You don't have permission to access "http://www.bybit.com/fiat/trade/otc/?" on this server.<p>
Reference #18.3f62645f.1657282455.2f87631

</p></body></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(f'user-agent={user_agent}')

它在我的本地机器上运行良好,但当我在Heroku服务器上部署它时,我遇到了同样的问题。
主要部分代码:

chrome_options = webdriver.ChromeOptions()
chrome_options.binary_location = os.environ.get("GOOGLE_CHROME_BIN")
chrome_options.add_argument("--headless")
chrome_options.add_argument("--disable-dev-shm-usage")
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--disable-gpu")
chrome_options.add_argument("--disable-features=NetworkService")
chrome_options.add_experimental_option('excludeSwitches', ['enable-logging'])

chrome_options.add_argument('--allow-running-insecure-content')
chrome_options.add_argument("window-size=800,600")
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(f'user-agent={user_agent}')

bybit_url = 'https://www.bybit.com/fiat/trade/otc/?actionType=0&token=USDT&fiat=RUB&paymentMethod=75'

driver.get(bybit_url)
sleep(5)
print(driver.page_source)
23c0lvtd

23c0lvtd1#

可能 * 访问被拒绝 *,因为Selenium驱动的 ChromeDriver 启动的google-chrome * 浏览上下文 * 被检测为bot
要避免检测,您需要使用参数--disable-blink-features=AutomationControlled,如下所示:

chrome_options.add_argument('--disable-blink-features=AutomationControlled')
lztngnrs

lztngnrs2#

我在Heroku上遇到了完全相同的问题,试图用 selenium 访问bybit站点。我使用了这个用户代理,它对我很有效:“Mozilla/5.0(X11; Linux x86_64)苹果网络工具包/537.36(KHTML,类似壁虎)Chrome浏览器/106.0.0.0Safari浏览器/537.36'

'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36'

在许多其他尝试和错误的尝试中,我比较了本地和Heroku上使用的用户代理,找到了这个用户代理。
本地:

from selenium import webdriver
import os

options = webdriver.ChromeOptions()
options.binary_location = os.environ.get( "GOOGLE_CHROME_BIN")
#options.add_argument("--headless")
options.add_argument("--disable-dev-shm-usage")
options.add_argument("--no-sandbox")
options.add_experimental_option('excludeSwitches', ['enable-logging'])
driver = webdriver.Chrome(options=options)
driver.get("https://www.bybit.com/fiat/trade/otc/?actionType=0&token=USDT&fiat=RUB&paymentMethod=75") 

print(driver.page_source)
print(driver.execute_script("return navigator.userAgent"))
driver.quit()

结果:

(...)</iframe></html>
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36

本地:

from selenium import webdriver
import os

options = webdriver.ChromeOptions()
options.binary_location = os.environ.get( "GOOGLE_CHROME_BIN")
options.add_argument("--headless")
options.add_argument("--disable-dev-shm-usage")
options.add_argument("--no-sandbox")
options.add_experimental_option('excludeSwitches', ['enable-logging'])
driver = webdriver.Chrome(options=options)
driver.get("https://www.bybit.com/fiat/trade/otc/?actionType=0&token=USDT&fiat=RUB&paymentMethod=75")

print(driver.page_source)
print(driver.execute_script("return navigator.userAgent"))
driver.quit()

结果:

<html><head>
<title>Access Denied</title>
</head><body>
<h1>Access Denied</h1>

You don t have permission to access "http://www.bybit.com/fiat/trade/otc/?" on this server.<p>       
Reference #18.17bd2f17.1664927640.82fbfed7

</p></body></html>
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/106.0.5249.91 Safari/537.36

关于Heroku:

from selenium import webdriver 
import os

options = webdriver.ChromeOptions()
options.binary_location = os.environ.get( "GOOGLE_CHROME_BIN")
options.add_argument("--headless")
options.add_argument("--disable-dev-shm-usage")
options.add_argument("--no-sandbox")
options.add_experimental_option('excludeSwitches', ['enable-logging'])
driver = webdriver.Chrome(options=options)
print(driver.execute_script("return navigator.userAgent"))
driver.quit()

结果:

Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/106.0.5249.91 Safari/537.36

关于Heroku:

from selenium import webdriver
import os

options = webdriver.ChromeOptions()
options.binary_location = os.environ.get( "GOOGLE_CHROME_BIN")
options.add_argument("--headless")
options.add_argument("--disable-dev-shm-usage")
options.add_argument("--no-sandbox")
options.add_experimental_option('excludeSwitches', ['enable-logging'])
user_agent ='Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36'
options.add_argument('user-agent={0}'.format(user_agent))
driver = webdriver.Chrome(options=options)
driver.get("https://www.bybit.com/fiat/trade/otc/?actionType=0&token=USDT&fiat=RUB&paymentMethod=75")

print(driver.page_source)
print(driver.execute_script("return navigator.userAgent"))
driver.quit()

获胜结果:

(...)</iframe></html>
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36

相关问题