前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >selenium设置chrome和phantomjs的请求头信息

selenium设置chrome和phantomjs的请求头信息

作者头像
十四君
发布2019-11-29 14:27:21
1.6K0
发布2019-11-29 14:27:21
举报
文章被收录于专栏:UrlteamUrlteam

出于反爬虫也好-跳转到手机端页面也好都需要设置请求头,那么如何进行呢?

目录

  • 一:selenium设置phantomjs请求头:
  • 二:selenium设置chrome请求头:
  • 三:selenium设置chrome–cookie:
  • 四:selenium设置phantomjs-图片不加载:

一:selenium设置phantomjs请求头:

可以复制下列代码运行,会访问https://httpbin.org/get?show_env=1  该网站能呈现你请求的头信息

来源于知乎回答

代码语言:javascript
复制
# !/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 感恩原作者

如代码

代码语言:javascript
复制
# !/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用于模拟登陆

代码语言:javascript
复制
# !/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-图片不加载:

代码语言:javascript
复制
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()

效果如图:

原创文章,转载请注明: 转载自URl-team

本文链接地址: selenium设置chrome和phantomjs的请求头信息

No related posts.

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-02-082,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 目录
  • 一:selenium设置phantomjs请求头:
  • 二:selenium设置chrome请求头:
  • 三:selenium设置chrome–cookie:
  • 四:selenium设置phantomjs-图片不加载:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档