1. 程式人生 > >selenium設定chrome和phantomjs的請求頭資訊

selenium設定chrome和phantomjs的請求頭資訊

出於反爬蟲也好-跳轉到手機端頁面也好都需要設定請求頭,那麼如何進行呢?

目錄

  • 一: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請求頭:

來源http://www.cnblogs.com/TTyb/p/6128323.html 感恩原作者

如程式碼

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

效果如圖:

996148-20161203114330787-1216998587

相關推薦

selenium設定chromephantomjs請求資訊

出於反爬蟲也好-跳轉到手機端頁面也好都需要設定請求頭,那麼如何進行呢? 目錄 一:selenium設定phantomjs請求頭: 二:selenium設定chrome請求頭: 三:selenium設定chrome–cookie: 四:seleniu

Centos下使用Selenium呼叫ChromePhantomJS的嘗試

centos下使用Selenium呼叫Chromedriver報錯 unknown error: DevToolsActivePort file doesn't exist 網上解決方案:         # options = Options()         #

HttpClient 傳送Post Get請求例子 包含設定請求資訊獲取返回資訊

                package com.test.action;import java.io.IOException;import java.util.ArrayList;import java.util.List;import org.apache.commons.httpclient.De

HttpClient 傳送Post Get請求例子(包含設定請求資訊獲取返回資訊

package com.test.action; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.apache.commons.httpclient.DefaultHt

java中使用selenium設定chrome啟動引數配置

1.給ChromeDriver配置禁止載入js,images ChromeOptions options = new ChromeOptions(); // 設定禁止載入項 Map<String, Object> prefs = new HashMap<String, Obje

Servlet 獲取請求資訊請求引數

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { /* * 獲取請求報文頭

jquery ajax請求時,設定請求資訊

設定一個名為 headers 的引數 參考程式碼: // attempt to make an XMLHttpRequest to indeed.com // jQuery 1.6.1 and

PHP curl設定請求資訊

需要設定 curl_setopt($ch,CURLOPT_HTTPHEADER,$header); $header是陣列形式 例子: $header=array( "Accept: application/json", "Content-Type: application

Selenium修改PhantomJS請求(Headers)

from selenium import webdriver from selenium.webdriver.common.desired_capabilities import DesiredCapabilities headers = { 'Accept': '

在AngularJs中怎麼設定請求資訊(headers)及不同方法的比較

在AngularJs中有三種方式可以設定請求頭資訊: 1、在http服務的在服務端發送請求時,也就是調用http()方法時,在config物件中設定請求頭資訊:事例如下: $http.post('/somePath' , someData , {

HTTP響應資訊請求資訊詳解

web效能測試中有一個web資源分析,web資源是從伺服器入手對web伺服器的效能進行分析。所以瞭解一下以下資訊是很有必要的哦。 一:響應頭資訊 HTTP(HyperTextTransferProtocol)是超文字傳輸協議的縮寫,它用於傳送WWW方式的資料,關於HT

HTTP響應請求資訊對照表

HTTP Request Header請求頭 Header 解釋 示例 Accept 指定客戶端能夠接收的內容型別 Accept: text/html,application/xhtm

小程序的getpost請求的區別

type www 成功 ava form brush quest -type function 小程序在使用wx.request()接口 時 header 請求頭默認是這樣的 wx.request({ url: ‘test.php‘, //僅為示例,並非真實的接口

easyPoi處理檔案下載檔名為空問題----請求資訊

導包:同上一篇匯出封裝請求引數 定義註解: import cn.afterturn.easypoi.excel.entity.enmus.ExcelType; import java.lang.annotation.*; /** * 匯出Excel註解. */ @Document

servlet學習(二)request物件獲取請求資訊

一、作用 封存了當前請求的所有請求資訊 二、使用 獲取請求頭的資訊包括: 1.請求行:請求方式 請求URL/URI 協議版本 //獲取請求行 String method=req.getMethod(); StringBuffer url=req.get

PHP接收http請求資訊

1、PHP 自帶函式 getallheaders() 目前 getallheaders() 只能用於 apache 中。如果想在 nginx 中也能使用,可以使用自定義函式。 foreach (getallheaders() as $name => $value)

header請求資訊詳細介紹

https://www.byvoid.com/zhs/blog/http-keep-alive-header HTTP協議頭部與Keep-Alive模式詳解   1、什麼是Keep-Alive模式? 我們知道HTTP協議採用“請求-應答”模式,當使用普通模式,即非KeepAlive模式時,每

接受http請求資訊(php)

文章來自:原始碼線上https://www.shengli.me/php/315.html   Php getallheaders函式   目前 getallheaders() 只能用於 apache 中。如果想在 nginx 中也能使用,可以使用自定義函式。

selenium設定代理(Phantomjs、Firefox、Chorme)

一、PhantomJs: from selenium import webdriver from selenium.webdriver.common.proxy import Proxy from selenium.webdriver.common.proxy imp

request資訊獲取request資訊的方法

頭資訊描述 Accept這個頭資訊指定瀏覽器或其他客戶端可以處理的 MIME 型別。值 image/png 或 image/jpeg 是最常見的兩種可能值。 Accept-Charset這個頭資訊指定瀏覽器可以用來顯示資訊的字符集。例如 ISO-8859-1。