目錄
html
一:selenium設置phantomjs請求頭:python
二:selenium設置chrome請求頭:web
三:selenium設置chrome--cookie:chrome
四:selenium設置phantomjs-圖片不加載:瀏覽器
能夠複製下列代碼運行,會訪問https://httpbin.org/get?show_env=1 該網站能呈現你請求的頭信息cookie
來源於知乎回答ide
# !/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()
來源selenium設置Chrome - TTyb - 博客園 感恩原做者網站
如代碼ui
# !/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()
cookie用於模擬登錄url
# !/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()
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() 效果如圖: