selenium 怎樣設置請求頭?

要爬個網站里數據,用selenium解析JS,請問selenium怎麼自定義請求頭。找了1小時資料,沒找到。selenium不支持么?
python還有其他可以解析JS的模塊么?要可以自定義請求頭的。


我寫一份完整版的吧包含selenium+phantomjs和selenium+chrome的
留了一份博客版的:selenium設置chrome和phantomjs的請求頭信息 | | URl-team

目錄

  • 一:selenium設置phantomjs請求頭:
  • 二:selenium設置chrome請求頭:
  • 三:selenium設置chrome--cookie:
  • 四:selenium設置phantomjs-圖片不載入:

一:selenium設置phantomjs請求頭:

可以複製下列代碼運行,會訪問https://httpbin.org/get?show_env=1 該網站能呈現你請求的頭信息

來源於知乎回答

# !/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請求頭:

來源selenium設置Chrome - TTyb - 博客園 感恩原作者

如代碼

# !/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()

效果如圖:


我用的Python3, 直接貼代碼, 暫時只會怎麼自定義UA, 見笑了, 不懂的歡迎追問~

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("http://m.iqiyi.com/v_19rrmmdbkg.html")

driver.get_screenshot_as_file("01.png")

driver.quit()


uaList = ["Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/13.10586",
"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36",
"Mozilla/5.0 (Windows NT 10.0; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0"]

dcap = dict(webdriver.DesiredCapabilities.PHANTOMJS)

dcap["phantomjs.page.settings.userAgent"] = (random.choice(uaList))


最近正好在用 selenium ,有修改 UA 和代理的需求,占坑。
參考:Change user agent for selenium driver @SO

Show Me Your Code

python 3.4

from selenium import webdriver

# 用於快速設置 profile 的代理信息的方法
def get_firefox_profile_with_proxy_set(profile, proxy_host):
# proxy_host
proxy_list = proxy_host.split(":")
agent_ip = proxy_list[0]
agent_port = proxy_list[1]

profile.set_preference("network.proxy.type", 1) # 使用代理
profile.set_preference("network.proxy.share_proxy_settings", True) # 所有協議公用一種代理配置
profile.set_preference("network.proxy.http", agent_ip)
profile.set_preference("network.proxy.http_port", int(agent_port))
profile.set_preference("network.proxy.ssl", agent_ip)
profile.set_preference("network.proxy.ssl_port", int(agent_port))
# 對於localhost的不用代理,這裡必須要配置,否則無法和 webdriver 通訊
profile.set_preference("network.proxy.no_proxies_on", "localhost,127.0.0.1")
profile.set_preference("network.http.use-cache", False)

return profile

# Selenium Settings - 設置代理 UA
# proxy 是形如 127.0.0.1:80 的代理字元串
# user_agent 是形如 "Mozilla/5.0 (X11; Linux x86_64; rv:58.0) Gecko/20100101 Firefox/58.0" 的 UA 字元串

# @FireFox
profile = webdriver.FirefoxProfile()
if proxy:
profile = get_firefox_profile_with_proxy_set(profile, proxy)
if user_agent:
profile.set_preference("general.useragent.override", user_agent)
driver = webdriver.Firefox(profile)

# @Chrome
chrome_options = webdriver.ChromeOptions()
if proxy:
chrome_options.add_argument("proxy-server=" + proxy)
if user_agent:
chrome_options.add_argument("user-agent=" + user_agent)
driver = webdriver.Chrome(chrome_options=chrome_options)

授人以魚,不如授人以漁

selenium 官網提供的一些瀏覽器的啟動項設置 browser-startup-manipulation ,更多瀏覽器啟動參數:

  • Chrome 可以設置的啟動參數列表:List of Chromium Command Line Switches,可以打開 chrome://flags 來查看當前應用的參數
  • FireFox 可以打開 about:config 來查看可以設置的選項

selenium設置火狐請求頭 有沒有呀 大神


Selenium PhantomJS custom headers in Python


我也在找這個問題,我用的是selenium和phantomjs,沒辦法自定義header,現在都準備換瀏覽器啦,你找到解決方案了嗎?


如何 設置 webdriver header?


搜索 selenium header,第一個結果:

webdriver - setting request headers in selenium


推薦閱讀:

Python 爬蟲調用 requests 如何設置代理(GoAgent/GoAgentX)?
分散式雲爬蟲,未來的主流採集方式?
如何爬取搜索引擎下某個關鍵字對應的所有網站?
為什麼這個網頁的源代碼用python爬下來後用beautifulsoup解析後會出現字元丟失?

TAG:Python | Selenium | 爬蟲計算機網路 | 網頁爬蟲 |