经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » HTML/CSS » 浏览器 » 查看文章
selenium设置proxy、headers的方法(phantomjs、Chrome、Firefox)
来源:jb51  时间:2018/11/30 9:16:36  对本文有异议

本文介绍了selenium设置proxy、headers的方法,把phantomjs、Chrome、Firefox几个浏览器的设置方法都总结一下,分享给大家,也给自己留个笔记

phantomjs

设置ip

方法1:

  1. service_args = [
  2. '--proxy=%s' % ip_html, # 代理 IP:prot (eg:192.168.0.28:808)
  3. '--proxy-type=http', # 代理类型:http/https
  4. ‘--load-images=no', # 关闭图片加载(可选)
  5. '--disk-cache=yes', # 开启缓存(可选)
  6. '--ignore-ssl-errors=true' # 忽略https错误(可选)
  7. ]
  8. driver = webdriver.PhantomJS(service_args=service_args)

方法2:

  1. browser=webdriver.PhantomJS(PATH_PHANTOMJS)
  2.  
  3. # 利用DesiredCapabilities(代理设置)参数值,重新打开一个sessionId,我看意思就相当于浏览器清空缓存后,加上代理重新访问一次url
  4. proxy=webdriver.Proxy()
  5. proxy.proxy_type=ProxyType.MANUAL
  6. proxy.http_proxy='1.9.171.51:800'
  7.  
  8. # 将代理设置添加到webdriver.DesiredCapabilities.PHANTOMJS中
  9. proxy.add_to_capabilities(webdriver.DesiredCapabilities.PHANTOMJS)
  10. browser.start_session(webdriver.DesiredCapabilities.PHANTOMJS)
  11. browser.get('http://1212.ip138.com/ic.asp')
  12.  
  13. print('1: ',browser.session_id)
  14. print('2: ',browser.page_source)
  15. print('3: ',browser.get_cookies())

还原为系统代理

  1. # 还原为系统代理
  2. proxy=webdriver.Proxy()
  3. proxy.proxy_type=ProxyType.DIRECT
  4. proxy.add_to_capabilities(webdriver.DesiredCapabilities.PHANTOMJS)
  5. browser.start_session(webdriver.DesiredCapabilities.PHANTOMJS)
  6. browser.get('http://1212.ip138.com/ic.asp')

设置请求头

方法2

  1. import random,requests,json
  2. from selenium import webdriver
  3. from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
  4. from selenium.webdriver.common.proxy import ProxyType
  5.  
  6.  
  7. #随机获取一个ip
  8. def proxies():
  9. r = requests.get("http://120.26.166.214:9840/JProxy/update/proxy/scoreproxy")
  10. rr = json.loads(r.text)
  11. hh = rr['ip'] + ":" + "8907"
  12. print(hh)
  13. return hh
  14. ips =proxies()
  15.  
  16.  
  17. #设置phantomjs请求头和代理方法一:
  18. #-------------------------------------------------------------------------------------
  19. # 设置代理
  20. service_args = [
  21. '--proxy=%s' % ips, # 代理 IP:prot (eg:192.168.0.28:808)
  22. '--ssl-protocol=any', #忽略ssl协议
  23. '--load - images = no', # 关闭图片加载(可选)
  24. '--disk-cache=yes', # 开启缓存(可选)
  25. '--ignore-ssl-errors=true' # 忽略https错误(可选)
  26. ]
  27.  
  28. #设置请求头
  29. user_agent = (
  30. "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) " +
  31. "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.57 Safari/537.36"
  32. )
  33. dcap = dict(DesiredCapabilities.PHANTOMJS)
  34. dcap["phantomjs.page.settings.userAgent"] = user_agent
  35. driver = webdriver.PhantomJS(executable_path=r"C:\soft\phantomjs-2.1.1-windows\bin\phantomjs.exe",
  36. desired_capabilities=dcap,service_args=service_args)
  37.  
  38. driver.get(url='http://www.baidu.com')
  39. page=driver.page_source
  40. print(page)
  41.  
  42. #设置phantomjs请求头和代理方法二:
  43. #-------------------------------------------------------------------------------------
  44. desired_capabilities = DesiredCapabilities.PHANTOMJS.copy()
  45. # 从USER_AGENTS列表中随机选一个浏览器头,伪装浏览器
  46. desired_capabilities["phantomjs.page.settings.userAgent"] = (random.choice('请求头池'))
  47.  
  48. # 不载入图片,爬页面速度会快很多
  49. desired_capabilities["phantomjs.page.settings.loadImages"] = False
  50.  
  51. # 利用DesiredCapabilities(代理设置)参数值,重新打开一个sessionId,我看意思就相当于浏览器清空缓存后,加上代理重新访问一次url
  52. proxy = webdriver.Proxy()
  53. proxy.proxy_type = ProxyType.MANUAL
  54. proxy.http_proxy = random.choice('ip池')
  55. proxy.add_to_capabilities(desired_capabilities)
  56. phantomjs_driver = r'C:\phantomjs-2.1.1-windows\bin\phantomjs.exe'
  57. # 打开带配置信息的phantomJS浏览器
  58. driver = webdriver.PhantomJS(executable_path=phantomjs_driver,desired_capabilities=desired_capabilities)
  59. driver.start_session(desired_capabilities)
  60.  
  61.  
  62. driver.get(url='http://www.baidu.com')
  63. page=driver.page_source
  64. print(page)
  65.  
  66.  
  67. # 隐式等待5秒,可以自己调节
  68. driver.implicitly_wait(5)
  69. # 设置10秒页面超时返回,类似于requests.get()的timeout选项,driver.get()没有timeout选项
  70. # 以前遇到过driver.get(url)一直不返回,但也不报错的问题,这时程序会卡住,设置超时选项能解决这个问题。
  71. driver.set_page_load_timeout(20)
  72. # 设置10秒脚本超时时间
  73. driver.set_script_timeout(20)
  74.  
  75.  
  76. #翻页命令
  77. driver.execute_script('window.scrollTo(0, document.body.scrollHeight)')

firefox

  1. import time
  2. from selenium.webdriver.common.proxy import*
  3.  
  4. myProxy = '202.202.90.20:8080'
  5.  
  6. proxy = Proxy({
  7. 'proxyType': ProxyType.MANUAL,
  8. 'httpProxy': myProxy,
  9. 'ftpProxy': myProxy,
  10. 'sslProxy': myProxy,
  11. 'noProxy': ''
  12. })
  13.  
  14. profile = webdriver.FirefoxProfile()
  15. if proxy:
  16. profile = get_firefox_profile_with_proxy_set(profile, proxy)
  17. if user_agent:
  18. profile.set_preference("general.useragent.override", user_agent)
  19.  
  20. driver=webdriver.Firefox(proxy=proxy,profile=profile)
  21. driver.get('https://www.baidu.com')
  22. time.sleep(3)
  23. driver.quit()
  24. firefox无头模式
  25. from selenium import webdriver
  26.  
  27. # 创建的新实例驱动
  28. options = webdriver.FirefoxOptions()
  29. #火狐无头模式
  30. options.add_argument('--headless')
  31. options.add_argument('--disable-gpu')
  32. # options.add_argument('window-size=1200x600')
  33.  
  34. executable_path='./source/geckodriver/geckodriver.exe'
  35. driver_path = webdriver.Firefox(firefox_options=options,executable_path=executable_path)

chrome

  1. # !/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3.  
  4. from selenium import webdriver
  5.  
  6. # 进入浏览器设置
  7. options = webdriver.ChromeOptions()
  8. #谷歌无头模式
  9. options.add_argument('--headless')
  10. options.add_argument('--disable-gpu')
  11. # options.add_argument('window-size=1200x600')
  12. # 设置中文
  13. options.add_argument('lang=zh_CN.UTF-8')
  14. # 更换头部
  15. 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"')
  16. #设置代理
  17. if proxy:
  18. options.add_argument('proxy-server=' + proxy)
  19. if user_agent:
  20. options.add_argument('user-agent=' + user_agent)
  21.  
  22. browser = webdriver.Chrome(chrome_options=options)
  23. url = "https://httpbin.org/get?show_env=1"
  24. browser.get(url)
  25. browser.quit()

 selenium设置chrome–cookie

  1. # !/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3.  
  4. from selenium import webdriver
  5. browser = webdriver.Chrome()
  6.  
  7. url = "https://www.baidu.com/"
  8. browser.get(url)
  9. # 通过js新打开一个窗口
  10. newwindow='window.open("https://www.baidu.com");'
  11. # 删除原来的cookie
  12. browser.delete_all_cookies()
  13. # 携带cookie打开
  14. browser.add_cookie({'name':'ABC','value':'DEF'})
  15. # 通过js新打开一个窗口
  16. browser.execute_script(newwindow)
  17. input("查看效果")
  18. browser.quit()
  19.  

selenium设置chrome-图片不加载

  1. from selenium import webdriver
  2.  
  3. options = webdriver.ChromeOptions()
  4. prefs = {
  5. 'profile.default_content_setting_values': {
  6. 'images': 2
  7. }
  8. }
  9. options.add_experimental_option('prefs', prefs)
  10. browser = webdriver.Chrome(chrome_options=options)
  11.  
  12. # browser = webdriver.Chrome()
  13. url = "http://image.baidu.com/"
  14. browser.get(url)
  15. input("是否有图")
  16. browser.quit()
  17.  

 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号