为什么instagram无法与Selenium无头Chrome一起使用? [英] Why doesn't instagram work with Selenium headless Chrome?

查看:101
本文介绍了为什么instagram无法与Selenium无头Chrome一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一个无头的insta机器人,但似乎找不到用户名和密码列(即NoSuchElementException).

I'm trying to build an insta bot that works headless, but it don't seem to find the username, password columns (i.e NoSuchElementException).

我试图运行此代码进行故障排除.(基本上会打开ig主页并对其进行截图)

I tried to run this code to troubleshoot. (which basicaly opens the ig homepage and screenshots it)

from selenium import webdriver
from time import sleep

options = webdriver.ChromeOptions()
options.headless = True
options.add_argument("--window-size=1920,1080")
browser = webdriver.Chrome(options=options)
browser.get("https://www.instagram.com")
browser.get_screenshot_as_file(f"screenshot.png")

然后我得到了这些屏幕截图基本上是用法语说错误,请在几分钟后重试"

and i got these screenshots basically saying 'error, retry after several minutes' in french

我尝试通过硒找到"connectez-vous"按钮,但我尝试的每个xpath均不起作用,并且无法通过f12找到它.

I tried finding the 'connectez-vous' button thru selenium, but every xpath i try doesn't work, and it's impossible to find it thru f12

该机器人随后将被上传到pythonanywhere,因此我可以在云中运行它(因此,如果您认为我可能会遇到其他问题,可以告诉我)

The bot will be later uploaded to pythonanywhere so i can run it in the cloud (so if you think i might run into some other problems you can let me know)

你建议我做什么?

推荐答案

from selenium import webdriver
from time import sleep

options = webdriver.ChromeOptions()
#options.headless = True
options.add_argument("--window-size=1920,1080")
options.add_argument("--headless")
options.add_argument("--disable-gpu")
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")
browser = webdriver.Chrome(options=options)
browser.get("https://www.instagram.com")
sleep(5)
#browser.refresh()
browser.get_screenshot_as_file(f"screenshot.png")

对于无头Chrome,将useragent设置为chromeheadless或其他内容,这使instagram能够检测到您正在使用无头chrome.

For headless chrome , useragent is set as chromeheadless or something , this makes instagram to detect that you are using headless chrome.

您可以通过指定硬编码的用户代理进行泄密,

You can vent this by specifying hardcoded useragent,

打开一个普通的chrome,转到网络"标签,打开请求标头,然后复制用户代理部分并替换为您的代码

open a normal chrome , goto network tab , open request header and copy the user agent part and replace in your code

无头浏览器检测

这篇关于为什么instagram无法与Selenium无头Chrome一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆