在 正式并入某大公司之后,网络必须设置为统一的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. 整合大量开源库项目(八)能够载入Gif动画的GifImageView

    转载请注明出处王亟亟的大牛之路 上周大多数时间都是依据兴起,想到什么做什么写了几个自己定义控件,把Soyi丢在那没怎么动,今天就把写的东西整合进来,顺便把SOyi"个人研发的结构理一下&qu ...

  2. CListCtrl自适应宽度

    原文链接: http://blog.csdn.net/benny5609/article/details/1967084 void CListCtrlExDlg::AdjustColumnWidth( ...

  3. Python 文件 read() 方法

    概述 Python 文件 read() 方法用于从文件中读取指定的字符数,如果未给定或为负则读取所有. 语法 read() 方法语法如下: fileObject.read([size]) 参数 siz ...

  4. Python 文件 fileno() 方法

    描述 Python 文件 fileno() 方法返回一个整型的文件描述符(file descriptor FD 整型),可用于底层操作系统的 I/O 操作. 语法 fileno() 方法语法如下: f ...

  5. 【转载并记录】SpringBoot 入门(一)

    https://blog.csdn.net/dhklsl/article/details/80309999 https://www.cnblogs.com/zheting/p/6707035.html ...

  6. Vue 点击波浪特效指令

  7. processing fill()和stroke()函数

    在procesiing有两个基本的函数,fill()和stroke()函数,这两个函数分别用来控制形状填充颜色和形状轮廓的颜色,fill()和stroke()可以接受的参数的个数为1,2,3.当参数的 ...

  8. Chris Richardson微服务实战系列

    微服务实战(一):微服务架构的优势与不足 微服务实战(二):使用API Gateway 微服务实战(三):深入微服务架构的进程间通信 微服务实战(四):服务发现的可行方案以及实践案例 微服务实践(五) ...

  9. win2016安装postgresql安装不了的问题

    我在阿里云的win2016服务器上下载postgresql,结果怎么都装不上. 双击 Exe没有 任何 反映 .. ... 网上搜索不出..在N个群里问 ,终于碰到有人和我一样的问题了..原来是阿里云 ...

  10. Socket网络编程--FTP客户端(2)(Windows)

    上一篇FTP客户端讲到如果制作一个简单的FTP客户端,功能实现了,但是后面我们发现了问题,就是FTP是使用明文进行操作的.对于普通情况来说就无所谓了.但有时候要安全的一点的话,就应该使用FTP的安全版 ...