Python urllib2 proxy
在 正式并入某大公司之后,网络必须设置为统一的proxy,好的方面没看到,但是立即让我一的一个小工具不能工作了。
在之前使用urllib2库,无需设置proxy,一切工作正常。在必须使用proxy之后,遇到了一系列的问题
1. 使用urllib2的proxy
import urllib2 enable_proxy = True
proxy_handler = urllib2.ProxyHandler({"http" : 'your_proxy'})
null_proxy_handler = urllib2.ProxyHandler({}) if enable_proxy:
opener = urllib2.build_opener(proxy_handler)
else:
opener = urllib2.build_opener(null_proxy_handler) urllib2.install_opener(opener)
结果得到错误如下:
URLError: <urlopen error [Errno 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond>
2. 使用requests
import requests
r = requests.get("http://www.google.com", proxies={"http": "your_proxy"})
print r.text
结果:
可以访问,但是内容不对
3. 使用requests的proxy
r = requests.get('http://www.thepage.com', proxies={"http":"your_proxy"})
print r.content
4. 使用https_proxy
import requests
proxies = {
"http": "your_http_proxy",
"https": "your_https_proxy",
}
r = requests.get("http://www.google.com", proxies=proxies)
print r.content
结果:
可以访问,内容不对
4. 使用selenium
from selenium import webdriver
driver = webdriver.Chrome()
driver.get(url)
html_resource = driver.page_source
结果:可以访问,内容正确
Python urllib2 proxy的更多相关文章
- python urllib2使用心得
python urllib2使用心得 1.http GET请求 过程:获取返回结果,关闭连接,打印结果 f = urllib2.urlopen(req, timeout=10) the_page = ...
- python urllib2 模拟网站登陆
python urllib2 模拟网站登陆 1. 可用浏览器先登陆,然后查看网页源码,分析登录表单 2. 使用python urllib2,cookielib 模拟网页登录 import urllib ...
- Python urllib2写爬虫时候每次request open以后一定要关闭
最近用python urllib2写一个爬虫工具,碰到运行一会程序后就会出现scoket connection peer reset错误.经过多次试验发现原来是在每次request open以后没有及 ...
- python扫描proxy并获取可用代理ip列表
mac或linux下可以work的代码如下: # coding=utf-8 import requests import re from bs4 import BeautifulSoup as bs ...
- python urllib2与urllib
1.urllib2可以接受一个Request对象,并以此可以来设置一个URL的headers,但是urllib只接收一个URL. 2.urllib模块可以提供进行urlencode的方法,该方法用于G ...
- python urllib2库的简单总结
urllib2的简单介绍参考网址:http://www.voidspace.org.uk/python/articles/urllib2.shtml Fetching URLsThe simplest ...
- python urllib2/urllib实现
urllib2和urllib是Python中的两个内置模块,要实现HTTP功能,实现方式是以urllib2为主,urllib为辅 urllib2提供一个基础函数urlopen,通过向指定的url发出请 ...
- python urllib2使用细节
刚好用到,这篇文章写得不错,转过来收藏. 转载自 道可道 | Python 标准库 urllib2 的使用细节 Python 标准库中有很多实用的工具类,但是在具体使用时,标准库文档上对使用细节 ...
- Python urllib2 调试
#!/usr/bin/env python # coding=utf-8 __author__ = 'zhaoyingnan' import urllib import urllib2 import ...
随机推荐
- Eclipse设置项目默认编码和换行符类型
为了实现不同操作系统间的Eclipse项目移植的一致性. 一.建议设置如下: 1. 默认的项目编码统一用UTF-8 2. 默认的换行符用UNIX类型 二.具体的配置点见下图:
- 温故而知新 原来 cheerio 还可以操作XML
- Hbase/Hadoop Java API编程常用语句
从scanner获取rowkey: for(Result rr : scanner){ String key =Bytes.toString(rr.getRow())} HBase API - Res ...
- python 取整的两种方法
问题简介: 要把一个浮点数(float)整数部分提取出来.比如把“2.1”变成“2”的这一过程:现在我们给这个过程起一个名字叫“取整”.那么它 在python中大致可以有两种写法 写法1)类型转换: ...
- ROC 曲线简要解释
阳性 (P, positive)阴性 (N, Negative)真阳性 (TP, true positive):正确的肯定.又称:命中 (hit)真阴性 (TN, true negative):正确的 ...
- MySQL的体系结构图
http://attachbak.dataguru.cn/attachments/forum/201408/30/231337wrbs3xpr5kbs3ryh.png
- php7+apache2.4 (Windows7下)安装
条件: ( 电脑必须win7 sp1, .netframework4 ) 一.下载php7和apache2.4 首先下载php7的windows压缩包,到这里下载http://windows.php. ...
- mysql定时执行某任务
查看event是否开启: show variables like '%sche%'; 将事件计划开启: set global event_scheduler=1; 关闭事件任务: alter even ...
- IOS开发之UIScrollVIew运用
UIScrollView可以实现在一个界面看到所有内容,同时也不需要担心所显示的内容超出屏幕的大小,当超出之后可以翻阅至下一页浏览. #pragma mark - UIScrollViewDelega ...
- 微信JSAPI 公众号支付 H5支付以及APP支付 WEBAPI接口开发测试
统一下单入口 调用该方法入口: public void WxPayAPI() { //string PayPrice ="99.9"; ////订单号 //string Payor ...