Python使用浏览器模拟访问页面之使用ip代理
最近需要使用浏览器模拟访问页面,同时需要使用不同的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代理的更多相关文章
- chrome浏览器 模拟访问移动端
谷歌Chrome浏览器,可以很方便地用来当3G手机模拟器.在Windows的[开始]-->[运行]中输入以下命令,启动谷歌浏览器,即可模拟相应手机的浏览器去访问3G手机网页: 谷歌Android ...
- Win10 Edge浏览器 应用商店 IE浏览器 无法访问页面 0x8000FFFF 问题解决
- 基于Python, Selenium, Phantomjs无头浏览器访问页面
引言: 在自动化测试以及爬虫领域,无头浏览器的应用场景非常广泛,本文将梳理其中的若干概念和思路,并基于代码示例其中的若干使用技巧. 1. 无头浏览器 通常大家在在打开网页的工具就是浏览器,通过界面上输 ...
- Python爬虫常用之登录(二) 浏览器模拟登录
浏览器模拟登录的主要技术点在于: 1.如何使用python的浏览器操作工具selenium 2.简单看一下网页,找到帐号密码对应的框框,要知道python开启的浏览器如何定位到这些 一.使用selen ...
- python 跑服务器,访问自己制作的简单页面
1 python 跑服务器,访问自己制作的简单页面 2 # win+b出现一个网址http:/0.0.1:5000/复制到浏览器查看# http://127.0.0.1:5000/home 做这个首 ...
- Python使用mechanize模拟浏览器
Python使用mechanize模拟浏览器 之前我使用自带的urllib2模拟浏览器去进行訪问网页等操作,非常多站点都会出错误,还会返回乱码.之后使用了 mechanize模拟浏览器,这些情况都没出 ...
- python 使用selenium模块爬取同一个url下不同页的内容(浏览器模拟人工翻页)
页面翻页,下一页可能是一个新的url 也有可能是用js进行页面跳转,url不变,解决方法是实现浏览器模拟人工翻页 目标:爬取同一个url下不同页的数据(上述第二种情况) url:http://www. ...
- 在PC机上,如何用Chrome浏览器模拟查看和调试手机的HTML5页面?
如题,如何用PC机上的Chrome浏览器模拟查看和调试手机HTML5页面? 参考操作步骤如下: 第一步.用Chrome打开要调试的页面: 第二步.按F12,打开“开发者工具”,点击其右上角的“Dock ...
- RF使用ie浏览器访问页面,浏览器启动只显示This is the initial start page for the WebDriver server,页面访问失败
问题描述:启动ie浏览器后,页面显示如下: 问题定位: 1.IE页面缩放没有设置成100% 2.ie浏览器的安全模式设置是否都将“启动保护模式”勾选上 3.iedriver驱动版本号是否和seleni ...
随机推荐
- Xcode使用心得01:断点中断问题和调整编译目标[转]
在obj-c系列博文里,我们粗浅的介绍了obj-c的一些语法以及F库中的一些标准类的使用,但是实际编写拿得出手的APP还是得老老实实在os x上用Xcode写啊!最近上网无意中发现还有支持os x和i ...
- 说说eclipse调优,缩短启动时间
初始配置: -startup plugins/org.eclipse.equinox.launcher_1.3.0.v20140415-2008.jar --launcher.library plug ...
- Linux 基础教程 30-tcpdump命令-2
在上一节讲了一些tcpdump常用功能,本期我们继续学习tcpdump后续功能. 流量过滤 tcpdump不仅支持单个过滤表达式过滤,也还支持多个过滤表达式.但需要注意的是传入的过滤表 ...
- (广搜)可口可乐 -- hdu -- 1495
链接: http://acm.hdu.edu.cn/showproblem.php?pid=1495 Time Limit: 2000/1000 MS (Java/Others) Memory ...
- (广搜)Catch That Cow -- poj -- 3278
链接: http://poj.org/problem?id=3278 Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6211 ...
- (3)-JSONObject的过滤设置
我们通常对一个json串和java对象进行互转时,经常会有选择性的过滤掉一些属性值.例如下面的类: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ...
- Python学习-6.Python的分支语句
Python的分支语句比较简单,只有if.else.elif三个关键字,也就是说,Python没有switch语句,而且,Python中并没有?:这个三目运算符. 例子: age = 18 if ag ...
- CURL命令测试网站打开速度
curl -o /dev/null -s -w %{time_namelookup}:%{time_connect}:%{time_starttransfer}:%{time_total} http: ...
- 分析SQL Server Profiler的监控方式
记得某次给一家公司调优的时候,负责人发给我一堆业务的T-SQL脚本,我面对海量脚本还是从容,虽然不了解内部复杂的业务,但是我们得专注问题的关键 “慢”,我们根据查询的“慢”把他们筛选出来,一一调式优化 ...
- linux squid
iptables -F iptables -F -t nat 网关 iptables -t nat -A POSTROUTING -s 10.1.0.0/16 -o eth0 -j MASQUERAD ...