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 ...
随机推荐
- android 控件各种颜色的半透明效果配置
格式: android:background="#XXxxxxxx"(颜色可以写在color中) 说明:半透明颜色值不同于平时使用的颜色,半透明颜色值共8位,前2位是透明度,后6位 ...
- 第2章 Python基础-字符编码&数据类型 字典 练习题
1.写代码,有如下字典,按照要求实现每一个功能,dic = {'k1':'v1','k2':'v2','k3':[11,22,33]} 请循环输出所有的 key dic = {'k1':'v1','k ...
- 简单示例:Spring4 整合 单个Redis服务
1. 引入spring-data-redis.jar API:https://docs.spring.io/spring-data/redis/docs/current/api/org/springf ...
- ReferenceError: weakly-referenced object no longer exists Python kafka
Python存入kafka报错,ReferenceError: weakly-referenced object no longer exists. Exception in thread 14: p ...
- iframe父页面获取iframe子页面的元素 与 iframe子页面获取父页面元素
一.在iframe子页面获取父页面元素代码如下:$('#objld', parent.document); 二.在父页面获取iframe子页面的元素代码如下:$("#objid", ...
- rabbitmq 二进制安装
# wget -Uvh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm # ls epel-rele ...
- centos7配置网桥
在Centos7上玩KVM的话一定要配置网桥的: [root@localhost nb]# ls /etc/sysconfig/network-scripts ifcfg-8866 ifdown if ...
- [Windows Azure] Getting Started with Windows Azure SQL Data Sync
Getting Started with Windows Azure SQL Data Sync In this tutorial, you learn the fundamentals of Win ...
- Vue.js使用-组件(上篇)
1.什么是组件 组件可以理解为定义的一个view模块,可重复使用. 2.组件使用 1)创建组件 var myComponent = Vue.extend({ template: ' this is a ...
- 每日英语:Got 5 Minutes? 'Flash Fiction' Catches On
Chinese author Lao Ma has a simple approach to his short stories: In the face of life, everything is ...