最近需要使用浏览器模拟访问页面,同时需要使用不同的ip访问,这个时候就考虑到在使用浏览器的同时加上ip代理。

本篇工作环境为win10,python3.6.

Chorme

使用Chrome浏览器模拟访问,代码如下

import time
from selenium import webdriver url = "https://www.cnblogs.com/"
driver = webdriver.Chrome("D:/tools/wedriver/chromedriver.exe")
driver.get(url)
time.sleep(2)
print(driver.title)
driver.close()

“D:/tools/wedriver/chromedriver.exe” 是下载的谷歌浏览器驱动,下载地址http://npm.taobao.org/mirrors/chromedriver/

chorme使用ip代理比较简单,使用如下代码即可

import time
from selenium import webdriver url = "https://www.baidu.com/s?wd=ip"
proxy = "118.190.217.182:80"
chromeOptions = webdriver.ChromeOptions() # 设置代理
chromeOptions.add_argument("--proxy-server=http://%s" % proxy)
driver = webdriver.Chrome("D:/tools/wedriver/chromedriver.exe", chrome_options=chromeOptions)
driver.get(url)
time.sleep(2)
print(driver.title)
driver.close()

得到的效果如下图:

可以见到百度查询到的本机ip已经改变。Chrome的这种代理方式中,访问使用http、https的网站都代理了。

Firefox

使用Firefox访问网页,代码如下:

import time
from selenium import webdriver url = "https://www.cnblogs.com/"
driver = webdriver.Firefox()
driver.get(url)
time.sleep(2)
print(driver.title)
driver.close()

直接这样运行会遇到以下错误:

selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH. 

需要装geckodriver,下载地址https://github.com/mozilla/geckodriver/releases。使用方式为,将对应版本geckodriver.exe放到python.exe的同目录下。

装好之后再次运行即可访问网站。

Firefox的ip代理较为麻烦,需要设置一些参数,具体如下

import time
from selenium import webdriver url = "https://www.baidu.com/s?wd=ip"
proxy = "118.190.217.182:80"
ip, port = proxy.split(':')
profile = webdriver.FirefoxProfile()
profile.set_preference('network.proxy.type', 1)
profile.set_preference('network.proxy.http', ip) # 设置http代理
profile.set_preference('network.proxy.http_port', int(port)) # 注意端口一定要使用数字而非字符串
profile.set_preference('network.proxy.ssl', ip) # 设置https代理
profile.set_preference('network.proxy.ssl_port', int(port))
profile.update_preferences()
driver = webdriver.Firefox(profile)
driver.get(url)
time.sleep(2)
print(driver.title)
driver.close()

这里有两个注意点:

1.当需要访问的网站为https时,一定要设置network.proxy.ssl参数才行

2.协议的端口号一定要是整数,不能直接使用字符串,如果拿到的是字符串就使用int转一下;我之前就是使用了字符串,一直代理不生效,以为哪里出了问题,磨了半天。。。

运行以上代码之后,得到的页面和上一张图相同,这里不再贴图。

整体代码如下:

# encoding=utf-8
# date: 2018/9/14
__Author__ = "Masako" import time
from selenium import webdriver def visit_web(url, proxy):
# chrome
# chromeOptions = webdriver.ChromeOptions() # 设置代理
# chromeOptions.add_argument("--proxy-server=http://%s" % proxy)
# driver = webdriver.Chrome("D:/tools/wedriver/chromedriver.exe", chrome_options=chromeOptions) # firefox
ip, port = proxy.split(':')
profile = webdriver.FirefoxProfile()
profile.set_preference('network.proxy.type', 1)
profile.set_preference('network.proxy.http', ip)
profile.set_preference('network.proxy.http_port', int(port)) # 注意端口一定要使用数字而非字符串
profile.set_preference('network.proxy.ssl', ip)
profile.set_preference('network.proxy.ssl_port', int(port))
profile.set_preference("network.proxy.share_proxy_settings", True)
profile.update_preferences()
driver = webdriver.Firefox(profile) driver.get(url)
time.sleep(2)
print(driver.title)
driver.delete_all_cookies() # 清除cookies
driver.close()
driver.quit() if __name__ == "__main__":
url = "https://www.baidu.com/s?wd=ip"
proxy = "118.190.217.182:80"
visit_web(url, proxy)

Python使用浏览器模拟访问页面之使用ip代理的更多相关文章

  1. chrome浏览器 模拟访问移动端

    谷歌Chrome浏览器,可以很方便地用来当3G手机模拟器.在Windows的[开始]-->[运行]中输入以下命令,启动谷歌浏览器,即可模拟相应手机的浏览器去访问3G手机网页: 谷歌Android ...

  2. Win10 Edge浏览器 应用商店 IE浏览器 无法访问页面 0x8000FFFF 问题解决

  3. 基于Python, Selenium, Phantomjs无头浏览器访问页面

    引言: 在自动化测试以及爬虫领域,无头浏览器的应用场景非常广泛,本文将梳理其中的若干概念和思路,并基于代码示例其中的若干使用技巧. 1. 无头浏览器 通常大家在在打开网页的工具就是浏览器,通过界面上输 ...

  4. Python爬虫常用之登录(二) 浏览器模拟登录

    浏览器模拟登录的主要技术点在于: 1.如何使用python的浏览器操作工具selenium 2.简单看一下网页,找到帐号密码对应的框框,要知道python开启的浏览器如何定位到这些 一.使用selen ...

  5. python 跑服务器,访问自己制作的简单页面

    1  python 跑服务器,访问自己制作的简单页面 2 # win+b出现一个网址http:/0.0.1:5000/复制到浏览器查看# http://127.0.0.1:5000/home 做这个首 ...

  6. Python使用mechanize模拟浏览器

    Python使用mechanize模拟浏览器 之前我使用自带的urllib2模拟浏览器去进行訪问网页等操作,非常多站点都会出错误,还会返回乱码.之后使用了 mechanize模拟浏览器,这些情况都没出 ...

  7. python 使用selenium模块爬取同一个url下不同页的内容(浏览器模拟人工翻页)

    页面翻页,下一页可能是一个新的url 也有可能是用js进行页面跳转,url不变,解决方法是实现浏览器模拟人工翻页 目标:爬取同一个url下不同页的数据(上述第二种情况) url:http://www. ...

  8. 在PC机上,如何用Chrome浏览器模拟查看和调试手机的HTML5页面?

    如题,如何用PC机上的Chrome浏览器模拟查看和调试手机HTML5页面? 参考操作步骤如下: 第一步.用Chrome打开要调试的页面: 第二步.按F12,打开“开发者工具”,点击其右上角的“Dock ...

  9. RF使用ie浏览器访问页面,浏览器启动只显示This is the initial start page for the WebDriver server,页面访问失败

    问题描述:启动ie浏览器后,页面显示如下: 问题定位: 1.IE页面缩放没有设置成100% 2.ie浏览器的安全模式设置是否都将“启动保护模式”勾选上 3.iedriver驱动版本号是否和seleni ...

随机推荐

  1. 10 Consensus and Profile

    Problem A matrix is a rectangular table of values divided into rows and columns. An m×nm×n matrix ha ...

  2. mongo学习- 副本集 大多数原则

    副本集中有一个重要的概念“大多数”,意思是说,选择主节点需要大多数决定(本人亲自做了实验) 步骤: 1.开启副本集(如果没有配置好 副本集的 亲参考我的上篇文章  https://www.cnblog ...

  3. java程序无法启动:Unsupported major.minor version 51.0

    今天在sae上部署了一个项目,结果总是出现503错误:service unavailable,然后jvm出现了一大串错误日志,如下 JAVA_SAE_Fatal_error: Failed start ...

  4. [label][JavaScript扩展] JavaSCript扩展

    http://www.idangero.us/sliders/swiper/ ,swipper for mobile terminal.

  5. Java定时任务的实现

    本例依据Java自身提供的接口实现,通过监听器(Listener)和定时器(Timer)定时执行某个任务(Task).专业的开源工具可参考Quartz:http://www.opensymphony. ...

  6. postgresql的日志实现机制

    1.事务的概念   事务是从实际生活中引入数据库的一个概念,即事务内的操作,要么全做,要么全不做.就像银行转账一样,当从一个帐户转出一部分钱之后,就必须在另一个帐户中存入相同数目的钱,若是转出钱之后, ...

  7. solr特点三: QueryElevation(编辑结果排序)

    在理想的情况下,搜索引擎只返回与用户查询相关的文档.而在现实的查询中,编辑(没发现更合适的表达)通常需要指定特定文档在搜索结果中的特定位置.这样做有很多原因.或许 “置顶” 的文档就是最好的查询结果. ...

  8. java提示找不到或无法加载主类

    背景 默许jdk的配置大家都没有问题,执行java,javac无报错,但今天在尝试在本地起来kafka的时候,提示java 找不到或无法加载主类,然后日志中提示 Files 找不到或无法加载主类:C: ...

  9. airport 抓包

    链接airport命令: ln -s /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources ...

  10. driver.get()和driver.navigate().to()到底有什么不同?-----Selenium快速入门(四)

    大家都知道,这两个方法都是跳转到指定的url地址,那么这两个方法有什么不同呢?遇到这种情况,第一反应就是查查官方的文档. 官方文档的说法是:Load a new web page in the cur ...