在 正式并入某大公司之后,网络必须设置为统一的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的更多相关文章

  1. python urllib2使用心得

    python urllib2使用心得 1.http GET请求 过程:获取返回结果,关闭连接,打印结果 f = urllib2.urlopen(req, timeout=10) the_page = ...

  2. python urllib2 模拟网站登陆

    python urllib2 模拟网站登陆 1. 可用浏览器先登陆,然后查看网页源码,分析登录表单 2. 使用python urllib2,cookielib 模拟网页登录 import urllib ...

  3. Python urllib2写爬虫时候每次request open以后一定要关闭

    最近用python urllib2写一个爬虫工具,碰到运行一会程序后就会出现scoket connection peer reset错误.经过多次试验发现原来是在每次request open以后没有及 ...

  4. python扫描proxy并获取可用代理ip列表

    mac或linux下可以work的代码如下: # coding=utf-8 import requests import re from bs4 import BeautifulSoup as bs ...

  5. python urllib2与urllib

    1.urllib2可以接受一个Request对象,并以此可以来设置一个URL的headers,但是urllib只接收一个URL. 2.urllib模块可以提供进行urlencode的方法,该方法用于G ...

  6. python urllib2库的简单总结

    urllib2的简单介绍参考网址:http://www.voidspace.org.uk/python/articles/urllib2.shtml Fetching URLsThe simplest ...

  7. python urllib2/urllib实现

    urllib2和urllib是Python中的两个内置模块,要实现HTTP功能,实现方式是以urllib2为主,urllib为辅 urllib2提供一个基础函数urlopen,通过向指定的url发出请 ...

  8. python urllib2使用细节

    刚好用到,这篇文章写得不错,转过来收藏.    转载自 道可道 | Python 标准库 urllib2 的使用细节 Python 标准库中有很多实用的工具类,但是在具体使用时,标准库文档上对使用细节 ...

  9. Python urllib2 调试

    #!/usr/bin/env python # coding=utf-8 __author__ = 'zhaoyingnan' import urllib import urllib2 import ...

随机推荐

  1. C#基础第七天-作业答案-利用面向对象的思想去实现名片-动态添加

    class Card { private string name; public string Name { get { return name; } set { name = value; } } ...

  2. 使用curator 来管理elasticsearch的index

    这里我们参考官网安装curator https://www.cookiesinn.org/elasticsearch_curator_delete_indices/ https://www.elast ...

  3. jQuery学习笔记(简介,选择器)

    jQuery优势 1. 强大的选择器.jQuery允许开发者使用从CSS1到CSS3几乎所有的选择器,以及jQuery独创的高级而复杂的选择器. 2. 出色的DOM操作封装 3. 可靠的事件处理机制 ...

  4. android 中毛玻璃效果的实现

    最近在做一款叫叽叽的App(男银懂的),其中有一个功能需要对图片处理实现毛玻璃的特效 进过一番预研,找到了3中实现方案,其中各有优缺点: 1.如果系统的api在16以上,可以使用系统提供的方法直接处理 ...

  5. [Windows Azure] Adding Sign-On to Your Web Application Using Windows Azure AD

    Adding Sign-On to Your Web Application Using Windows Azure AD 14 out of 19 rated this helpful - Rate ...

  6. (原创)C++11改进我们的程序之简化我们的程序(六)

    这次要讲的内容是:c++11中的lamda表达式. lamda表达式是我最喜欢的一个c++11特性之一,在我的代码中随处可见它的身影,其实在c#3.5中就引入了lamda,java中至今还没引入,要等 ...

  7. (原创)c++11改进我们的模式之改进单例模式

    我会写关于c++11的一个系列的文章,会讲到如何使用c++11改进我们的程序,本次讲如何改进我们的模式,会讲到如何改进单例模式.观察者模式.访问者模式.工厂模式.命令模式等模式.通过c++11的改进, ...

  8. linux命令(46):程序运行前后台切换

    A,Shell支持作用控制,有以下命令:1. command& 让进程在后台运行2. jobs 查看后台运行的进程3. fg %n 让后台运行的进程n到前台来4. bg %n 让进程n到后台去 ...

  9. Mac下用brew搭建PHP(LNMP/LAMP)开发环境

    Mac下搭建lamp开发环境很容易,有xampp和mamp现成的集成环境.但是集成环境对于经常需要自定义一些配置的开发者来说会非常麻烦,而且Mac本身自带apache和php,在brew的帮助下非常容 ...

  10. profile bashrc bash_profile 之间的区别和联系

    /etc/profile:此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行.并从/etc/profile.d目录的配置文件中搜集shell的设置. /etc/bashrc:为每一个 ...