Python爬虫:selenium使用chrome和PhantomJS实用参数

版权声明:本文为博主原创文章,欢迎转载,请注明出处 https://blog.csdn.net/mouday/article/details/82049271

chrome参数设置示例


from selenium import webdriver


options = webdriver.ChromeOptions()

# 设置语言
options.add_argument('lang=zh_CN.UTF-8')

# 不显示界面
options.add_argument('headless')

# 设置user-agent请求头
options.add_argument('user-agent=%s' % user_agent)

 # 设置代理        
options.add_argument('--proxy-server= %s' % proxy)
# http://127.0.0.1

# 图片不加载
prefs = {
    'profile.default_content_setting_values': {
        'images': 2
    }
}
options.add_experimental_option('prefs', prefs)

browser = webdriver.Chrome(chrome_options = options)

# 设置超时
browser.set_page_load_timeout(5)
browser.set_script_timeout(10)  # 这两种设置都进行才有效

# 设置窗口大小
browser.set_window_size(1366, 768)

# 访问网站
browser.get("https://www.baidu.com/")

# 截图
browser.save_screenshot("1.png")  

参数设置

–user-data-dir=”[PATH]” 指定用户文件夹User Data路径,可以把书签这样的用户数据保存在系统分区以外的分区。 
–disk-cache-dir=”[PATH]“ 指定缓存Cache路径 
–disk-cache-size= 指定Cache大小,单位Byte 
–first run 重置到初始状态,第一次运行 
–incognito 隐身模式启动 
–disable-javascript 禁用Javascript 
–omnibox-popup-count=”num” 将地址栏弹出的提示菜单数量改为num个。我都改为15个了。 
–user-agent=”xxxxxxxx” 修改HTTP请求头部的Agent字符串,可以通过about:version页面查看修改效果 
–disable-plugins 禁止加载所有插件,可以增加速度。可以通过about:plugins页面查看效果 
–disable-javascript 禁用JavaScript,如果觉得速度慢在加上这个 
–disable-java 禁用java 
–start-maximized 启动就最大化 
–no-sandbox 取消沙盒模式 
–single-process 单进程运行 
–process-per-tab 每个标签使用单独进程 
–process-per-site 每个站点使用单独进程 
–in-process-plugins 插件不启用单独进程 
–disable-popup-blocking 禁用弹出拦截 
–disable-plugins 禁用插件 
–disable-images 禁用图像 
–incognito 启动进入隐身模式 
–enable-udd-profiles 启用账户切换菜单 
–proxy-pac-url 使用pac代理 [via 1/2] 
–lang=zh-CN 设置语言为简体中文 
–disk-cache-dir 自定义缓存目录 
–disk-cache-size 自定义缓存最大值(单位byte) 
–media-cache-size 自定义多媒体缓存最大值(单位byte) 
–bookmark-menu 在工具 栏增加一个书签按钮 
–enable-sync 启用书签同步 
–single-process 单进程运行Google Chrome 
–start-maximized 启动Google Chrome就最大化 
–disable-java 禁止Java 
–no-sandbox 非沙盒模式运行
  

phantomJS参数设置示例


from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.common.proxy import ProxyType


dcap = dict(DesiredCapabilities.PHANTOMJS)

# 设置user-agent请求头
dcap["phantomjs.page.settings.userAgent"] = user_agent

# 禁止加载图片
dcap["phantomjs.page.settings.loadImages"] = False

phantomJS = webdriver.PhantomJS(desired_capabilities=dcap)

# 设置代理
_proxy = webdriver.Proxy()
_proxy.proxy_type = ProxyType.MANUAL
_proxy.http_proxy = proxy

# 将代理设置添加到webdriver.DesiredCapabilities.PHANTOMJS中
 _proxy.add_to_capabilities(webdriver.DesiredCapabilities.PHANTOMJS)

phantomJS.start_session(webdriver.DesiredCapabilities.PHANTOMJS)

参考:
selenium+python配置chrome浏览器的选项

猜你喜欢

转载自blog.csdn.net/mouday/article/details/82049271