最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

selenium設(shè)置proxy、headers的方法(phantomjs、Chrome、Firefox)

 更新時(shí)間:2018年11月29日 14:43:10   作者:周小董  
這篇文章主要介紹了selenium設(shè)置proxy、headers的方法(phantomjs、Chrome、Firefox),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

本文介紹了selenium設(shè)置proxy、headers的方法,把phantomjs、Chrome、Firefox幾個(gè)瀏覽器的設(shè)置方法都總結(jié)一下,分享給大家,也給自己留個(gè)筆記

phantomjs

設(shè)置ip

方法1:

service_args = [
  '--proxy=%s' % ip_html,  # 代理 IP:prot  (eg:192.168.0.28:808)
  '--proxy-type=http',      # 代理類型:http/https
  ‘--load-images=no',      # 關(guān)閉圖片加載(可選)
  '--disk-cache=yes',      # 開啟緩存(可選)
  '--ignore-ssl-errors=true'  # 忽略https錯(cuò)誤(可選)
]
driver = webdriver.PhantomJS(service_args=service_args)

方法2:

browser=webdriver.PhantomJS(PATH_PHANTOMJS)

# 利用DesiredCapabilities(代理設(shè)置)參數(shù)值,重新打開一個(gè)sessionId,我看意思就相當(dāng)于瀏覽器清空緩存后,加上代理重新訪問一次url
proxy=webdriver.Proxy()
proxy.proxy_type=ProxyType.MANUAL
proxy.http_proxy='1.9.171.51:800'

# 將代理設(shè)置添加到webdriver.DesiredCapabilities.PHANTOMJS中
proxy.add_to_capabilities(webdriver.DesiredCapabilities.PHANTOMJS)
browser.start_session(webdriver.DesiredCapabilities.PHANTOMJS)
browser.get('http://1212.ip138.com/ic.asp')

print('1: ',browser.session_id)
print('2: ',browser.page_source)
print('3: ',browser.get_cookies())

還原為系統(tǒng)代理

# 還原為系統(tǒng)代理
proxy=webdriver.Proxy()
proxy.proxy_type=ProxyType.DIRECT
proxy.add_to_capabilities(webdriver.DesiredCapabilities.PHANTOMJS)
browser.start_session(webdriver.DesiredCapabilities.PHANTOMJS)
browser.get('http://1212.ip138.com/ic.asp')

設(shè)置請求頭

方法2

import random,requests,json
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.common.proxy import ProxyType


#隨機(jī)獲取一個(gè)ip
def proxies():
  r = requests.get("http://120.26.166.214:9840/JProxy/update/proxy/scoreproxy")
  rr = json.loads(r.text)
  hh = rr['ip'] + ":" + "8907"
  print(hh)
  return hh
ips =proxies()


#設(shè)置phantomjs請求頭和代理方法一:
#-------------------------------------------------------------------------------------
# 設(shè)置代理
service_args = [
  '--proxy=%s' % ips, # 代理 IP:prot  (eg:192.168.0.28:808)
  '--ssl-protocol=any',      #忽略ssl協(xié)議
  '--load - images = no',     # 關(guān)閉圖片加載(可選)
  '--disk-cache=yes',       # 開啟緩存(可選)
  '--ignore-ssl-errors=true'   # 忽略https錯(cuò)誤(可選)
]

#設(shè)置請求頭
user_agent = (
  "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) " +
  "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.57 Safari/537.36"
  )
dcap = dict(DesiredCapabilities.PHANTOMJS)
dcap["phantomjs.page.settings.userAgent"] = user_agent
driver = webdriver.PhantomJS(executable_path=r"C:\soft\phantomjs-2.1.1-windows\bin\phantomjs.exe",
               desired_capabilities=dcap,service_args=service_args)

driver.get(url='http://www.baidu.com')
page=driver.page_source
print(page)

#設(shè)置phantomjs請求頭和代理方法二:
#-------------------------------------------------------------------------------------
desired_capabilities = DesiredCapabilities.PHANTOMJS.copy()
# 從USER_AGENTS列表中隨機(jī)選一個(gè)瀏覽器頭,偽裝瀏覽器
desired_capabilities["phantomjs.page.settings.userAgent"] = (random.choice('請求頭池'))

# 不載入圖片,爬頁面速度會快很多
desired_capabilities["phantomjs.page.settings.loadImages"] = False

# 利用DesiredCapabilities(代理設(shè)置)參數(shù)值,重新打開一個(gè)sessionId,我看意思就相當(dāng)于瀏覽器清空緩存后,加上代理重新訪問一次url
proxy = webdriver.Proxy()
proxy.proxy_type = ProxyType.MANUAL
proxy.http_proxy = random.choice('ip池')
proxy.add_to_capabilities(desired_capabilities)
phantomjs_driver = r'C:\phantomjs-2.1.1-windows\bin\phantomjs.exe'
# 打開帶配置信息的phantomJS瀏覽器
driver = webdriver.PhantomJS(executable_path=phantomjs_driver,desired_capabilities=desired_capabilities)
driver.start_session(desired_capabilities)


driver.get(url='http://www.baidu.com')
page=driver.page_source
print(page)


# 隱式等待5秒,可以自己調(diào)節(jié)
driver.implicitly_wait(5)
# 設(shè)置10秒頁面超時(shí)返回,類似于requests.get()的timeout選項(xiàng),driver.get()沒有timeout選項(xiàng)
# 以前遇到過driver.get(url)一直不返回,但也不報(bào)錯(cuò)的問題,這時(shí)程序會卡住,設(shè)置超時(shí)選項(xiàng)能解決這個(gè)問題。
driver.set_page_load_timeout(20)
# 設(shè)置10秒腳本超時(shí)時(shí)間
driver.set_script_timeout(20)

 

#翻頁命令
driver.execute_script('window.scrollTo(0, document.body.scrollHeight)')
 

firefox

import time 
from selenium.webdriver.common.proxy import* 

myProxy = '202.202.90.20:8080'

proxy = Proxy({
 'proxyType': ProxyType.MANUAL, 
 'httpProxy': myProxy, 
 'ftpProxy': myProxy, 
 'sslProxy': myProxy, 
 'noProxy': ''
 })

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(proxy=proxy,profile=profile) 
driver.get('https://www.baidu.com') 
time.sleep(3) 
driver.quit()
 
firefox無頭模式
from selenium import webdriver

# 創(chuàng)建的新實(shí)例驅(qū)動
options = webdriver.FirefoxOptions()
#火狐無頭模式
options.add_argument('--headless')
options.add_argument('--disable-gpu')
# options.add_argument('window-size=1200x600')

executable_path='./source/geckodriver/geckodriver.exe'
driver_path = webdriver.Firefox(firefox_options=options,executable_path=executable_path)

chrome

# !/usr/bin/python
# -*- coding: utf-8 -*-

from selenium import webdriver

# 進(jìn)入瀏覽器設(shè)置
options = webdriver.ChromeOptions()
#谷歌無頭模式
options.add_argument('--headless')
options.add_argument('--disable-gpu')
# options.add_argument('window-size=1200x600')
# 設(shè)置中文
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"')
#設(shè)置代理
if proxy:
  options.add_argument('proxy-server=' + proxy)
if user_agent:
  options.add_argument('user-agent=' + user_agent)

browser = webdriver.Chrome(chrome_options=options)
url = "https://httpbin.org/get?show_env=1"
browser.get(url)
browser.quit()

 selenium設(shè)置chrome–cookie

# !/usr/bin/python
# -*- coding: utf-8 -*-

from selenium import webdriver
browser = webdriver.Chrome()

url = "https://www.baidu.com/"
browser.get(url)
# 通過js新打開一個(gè)窗口
newwindow='window.open("https://www.baidu.com");'
# 刪除原來的cookie
browser.delete_all_cookies()
# 攜帶cookie打開
browser.add_cookie({'name':'ABC','value':'DEF'})
# 通過js新打開一個(gè)窗口
browser.execute_script(newwindow)
input("查看效果")
browser.quit()

selenium設(shè)置chrome-圖片不加載

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()

 以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python使用pyautocad+openpyxl處理cad文件示例

    Python使用pyautocad+openpyxl處理cad文件示例

    這篇文章主要介紹了Python使用pyautocad+openpyxl處理cad文件,結(jié)合實(shí)例形式分析了Python使用pyautocad與openpyxl模塊讀寫cad文件相關(guān)應(yīng)用操作技巧,需要的朋友可以參考下
    2019-07-07
  • Python如何讀寫二進(jìn)制數(shù)組數(shù)據(jù)

    Python如何讀寫二進(jìn)制數(shù)組數(shù)據(jù)

    這篇文章主要介紹了Python如何讀寫二進(jìn)制數(shù)組數(shù)據(jù),文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-08-08
  • 在jupyter notebook 添加 conda 環(huán)境的操作詳解

    在jupyter notebook 添加 conda 環(huán)境的操作詳解

    這篇文章主要介紹了在jupyter notebook 添加 conda 環(huán)境的操作詳解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-04-04
  • python 爬蟲爬取京東ps4售賣情況

    python 爬蟲爬取京東ps4售賣情況

    這篇文章主要介紹了python 如何用爬蟲爬取京東ps4售賣情況,幫助大家更好的利用python爬取自己想要的數(shù)據(jù),感興趣的朋友可以了解下
    2020-12-12
  • Python進(jìn)階之尾遞歸的用法實(shí)例

    Python進(jìn)階之尾遞歸的用法實(shí)例

    本篇文章主要介紹了Python進(jìn)階之尾遞歸的用法實(shí)例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-01-01
  • Vs Code中8個(gè)好用的python 擴(kuò)展插件

    Vs Code中8個(gè)好用的python 擴(kuò)展插件

    這篇文章主要介紹了Vs Code中8個(gè)好用的python 擴(kuò)展插件,幫助大家更好的利用vs code進(jìn)行python開發(fā),感興趣的朋友可以了解下
    2020-10-10
  • Python實(shí)現(xiàn)各種郵件發(fā)送

    Python實(shí)現(xiàn)各種郵件發(fā)送

    這篇文章主要介紹了Python實(shí)現(xiàn)各種郵件發(fā)送,Python內(nèi)置對SMTP的支持,可以發(fā)送純文本郵件、HTML郵件以及帶附件的郵件,下文詳細(xì)實(shí)現(xiàn)過程需要的小伙伴可以參考一下
    2022-05-05
  • pycharm?sql語句警告的處理

    pycharm?sql語句警告的處理

    這篇文章主要介紹了pycharm?sql語句警告的處理方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • 學(xué)會這29個(gè)常用函數(shù),你就是Pandas專家

    學(xué)會這29個(gè)常用函數(shù),你就是Pandas專家

    Pandas?無疑是?Python?處理表格數(shù)據(jù)最好的庫之一,但是很多新手無從下手,這里總結(jié)出最常用的?29?個(gè)函數(shù),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2022-11-11
  • python輸出第n個(gè)默尼森數(shù)的實(shí)現(xiàn)示例

    python輸出第n個(gè)默尼森數(shù)的實(shí)現(xiàn)示例

    這篇文章主要介紹了python輸出第n個(gè)默尼森數(shù)的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-03-03

最新評論

全南县| 麻城市| 安宁市| 德钦县| 垫江县| 民乐县| 峨眉山市| 西华县| 黔西县| 福贡县| 修水县| 垦利县| 荆门市| 辽宁省| 涿州市| 小金县| 饶平县| 华宁县| 临朐县| 辛集市| 贵阳市| 宜兴市| 望谟县| 鹿泉市| 荆门市| 海原县| 日土县| 正定县| 永州市| 富川| 内黄县| 辛集市| 上虞市| 三都| 繁昌县| 赫章县| 临夏市| 新化县| 南江县| 柯坪县| 景泰县|