Chrome浏览器初始化

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time

options = webdriver.ChromeOptions()
options.add_argument('--disable-blink-features=AutomationControlled')  # 禁用浏览器的自动化特性
options.add_experimental_option('excludeSwitches', ['enable-automation'])  # 排除浏览器的自动化开关选项
options.add_experimental_option('useAutomationExtension', False)  # 禁用浏览器的自动化扩展
#options.add_argument('--headless')    # 无头模式,不显示浏览器窗口
options.add_argument('--no-sandbox')  # 禁用沙盒模式
wd = webdriver.Chrome(options=options)  # 按照上面的设置选项启动浏览器
wd.implicitly_wait(10)

# 打开网站
url = 'https://iam.pt.ouchn.cn/am/UI/Login?realm=%2F&service=initService&goto=https%3A%2F%2Fiam.pt.ouchn.cn%2Fam%2Foauth2%2Fauthorize%3Fservice%3DinitService%26response_type%3Dcode%26client_id%3D345fcbaf076a4f8a%26scope%3Dall%26redirect_uri%3Dhttps%253A%252F%252Fmenhu.pt.ouchn.cn%252Fouchnapp%252Fwap%252Flogin%252Findex%26decision%3DAllow/'
wd.get(url)
time.sleep(3)# 等待网站加载完成

伪装请求内容

上面这段代码并没有伪装请求,它只是使用了一些Chrome浏览器的选项来禁用浏览器的自动化特性和扩展,以及禁用沙盒模式等。如果要伪装请求,可以使用Python中的requests库或者Scrapy框架来发送请求,并设置请求头部信息来模拟正常的浏览器请求。例如:

import requests

url = 'https://iam.pt.ouchn.cn/am/UI/Login?realm=%2F&service=initService&goto=https%3A%2F%2Fiam.pt.ouchn.cn%2Fam%2Foauth2%2Fauthorize%3Fservice%3DinitService%26response_type%3Dcode%26client_id%3D345fcbaf076a4f8a%26scope%3Dall%26redirect_uri%3Dhttps%253A%252F%252Fmenhu.pt.ouchn.cn%252Fouchnapp%252Fwap%252Flogin%252Findex%26decision%3DAllow/'

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 Edge/16.16299',
    'Referer': 'https://iam.pt.ouchn.cn/am/UI/Login?realm=%2F&service=initService&goto=https%3A%2F%2Fiam.pt.ouchn.cn%2Fam%2Foauth2%2Fauthorize%3Fservice%3DinitService%26response_type%3Dcode%26client_id%3D345fcbaf076a4f8a%26scope%3Dall%26redirect_uri%3Dhttps%253A%252F%252Fmenhu.pt.ouchn.cn%252Fouchnapp%252Fwap%252Flogin%252Findex%26decision%3DAllow/',
    'Accept-Language': 'zh-CN,zh;q=0.9',
    'Accept-Encoding': 'gzip, deflate, br',
    'Connection': 'keep-alive'
}

response = requests.get(url, headers=headers)
print(response.text)
Chrome浏览器初始化及请求伪装

原文地址: https://www.cveoy.top/t/topic/jpBj 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录