日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

pythonselenium设置_selenium 怎样设置请求头?

發布時間:2024/10/14 python 69 豆豆
生活随笔 收集整理的這篇文章主要介紹了 pythonselenium设置_selenium 怎样设置请求头? 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

我寫一份完整版的吧包含selenium+phantomjs和selenium+chrome的

留了一份博客版的:selenium設置chrome和phantomjs的請求頭信息 | | URl-team

目錄一:selenium設置phantomjs請求頭:

二:selenium設置chrome請求頭:

三:selenium設置chrome--cookie:

四:selenium設置phantomjs-圖片不加載:

一:selenium設置phantomjs請求頭:

來源于知乎回答

# !/usr/bin/python

# -*- coding: utf-8 -*-

from selenium import webdriver

from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

dcap = dict(DesiredCapabilities.PHANTOMJS)

dcap["phantomjs.page.settings.userAgent"] = (

"Mozilla/5.0 (Linux; Android 5.1.1; Nexus 6 Build/LYZ28E) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.23 Mobile Safari/537.36"

)

driver = webdriver.PhantomJS(desired_capabilities=dcap)

driver.get("https://httpbin.org/get?show_env=1")

driver.get_screenshot_as_file('01.png')

driver.quit()

作者:JIM LIU

鏈接:https://www.zhihu.com/question/35547395/answer/106652782

來源:知乎

著作權歸作者所有,轉載請聯系作者獲得授權。

二:selenium設置chrome請求頭:

如代碼

# !/usr/bin/python

# -*- coding: utf-8 -*-

from selenium import webdriver

# 進入瀏覽器設置

options = webdriver.ChromeOptions()

# 設置中文

options.add_argument('lang=zh_CN.UTF-8')

# 更換頭部

options.add_argument('user-agent="Mozilla/5.0 (iPod; U; CPU iPhone OS 2_1 like Mac OS X; ja-jp) AppleWebKit/525.18.1 (KHTML, like Gecko) Version/3.1.1 Mobile/5F137 Safari/525.20"')

browser = webdriver.Chrome(chrome_options=options)

url = "https://httpbin.org/get?show_env=1"

browser.get(url)

browser.quit()

三:selenium設置chrome--cookie:

cookie用于模擬登陸

# !/usr/bin/python

# -*- coding: utf-8 -*-

from selenium import webdriver

browser = webdriver.Chrome()

url = "https://www.baidu.com/"

browser.get(url)

# 通過js新打開一個窗口

newwindow='window.open("https://www.baidu.com");'

# 刪除原來的cookie

browser.delete_all_cookies()

# 攜帶cookie打開

browser.add_cookie({'name':'ABC','value':'DEF'})

# 通過js新打開一個窗口

browser.execute_script(newwindow)

input("查看效果")

browser.quit()

四:selenium設置phantomjs-圖片不加載:

from selenium import webdriver

options = webdriver.ChromeOptions()

prefs = {

'profile.default_content_setting_values': {

'images': 2

}

}

options.add_experimental_option('prefs', prefs)

browser = webdriver.Chrome(chrome_options=options)

# browser = webdriver.Chrome()

url = "http://image.baidu.com/"

browser.get(url)

input("是否有圖")

browser.quit()

效果如圖:

總結

以上是生活随笔為你收集整理的pythonselenium设置_selenium 怎样设置请求头?的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。