urllib2

urllib and “SSL: CERTIFICATE_VERIFY_FAILED” Error

匿名 (未验证) 提交于 2019-12-03 01:29:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I am getting this error Exception in thread Thread-3: Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 810, in __bootstrap_inner self.run() File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 763, in run self.__target(*self.__args, **self.__kwargs) File "/Users/Matthew/Desktop/Skypebot 2.0/bot.py", line 271, in process info = urllib2.urlopen(req).read() File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2

Python urllib2 > HTTP Proxy > HTTPS request

匿名 (未验证) 提交于 2019-12-03 01:20:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: This work fine: import urllib2 opener = urllib2.build_opener( urllib2.HTTPHandler(), urllib2.HTTPSHandler(), urllib2.ProxyHandler({'http': 'http://user:pass@proxy:3128'})) urllib2.install_opener(opener) print urllib2.urlopen('http://www.google.com').read() But, if http change to https : ... print urllib2.urlopen('https://www.google.com').read() There are errors: Traceback (most recent call last): File "D:\Temp\6\tmp.py", line 13, in print urllib2.urlopen('https://www.google.com').read() File "C:\Python26\lib\urllib2.py", line 124, in urlopen

How to Speed Up Python's urllib2 when doing multiple requests

匿名 (未验证) 提交于 2019-12-03 01:12:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I am making several http requests to a particular host using python's urllib2 library. Each time a request is made a new tcp and http connection is created which takes a noticeable amount of time. Is there any way to keep the tcp/http connection alive using urllib2? 回答1: If you switch to httplib , you will have finer control over the underlying connection. For example: import httplib conn = httplib.HTTPConnection(url) conn.request('GET', '/foo') r1 = conn.getresponse() r1.read() conn.request('GET', '/bar') r2 = conn.getresponse() r2.read()

Downloading HTTPS pages with urllib, error:14077438:SSL routines:SSL23_GET_SERVER_HELLO:tlsv1 alert internal error

匿名 (未验证) 提交于 2019-12-03 00:50:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Im using newest Kubuntu with Python 2.7.6 . I try to download a https page using the below code: import urllib2 hdr = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3', 'Accept-Encoding': 'none', 'Accept-Language': 'pl-PL,pl;q=0.8', 'Connection': 'keep-alive'} req = urllib2.Request(main_page_url, headers=hdr) try: page = urllib2.urlopen(req)

urllib和urllib2区别

匿名 (未验证) 提交于 2019-12-03 00:26:01
urllib和urllib2区别  urllib和urllib2区别:    Python的urllib和urllib2模块都做与请求URL相关的操作,但他们提供不同的功能。他们两个最显着的差异如下:   总结1:urllib2可以接受一个Request对象,并以此可以来设置一个URL的headers,但是urllib只接收一个URL。这意味着,你不能伪装你的用户代理字符串等。   总结2:urllib模块可以提供进行urlencode的方法,该方法用于GET查询字符串的生成,urllib2的不具有这样的功能。这就是urllib与urllib2经常在一起使用的原因。       A.urllib2概述   urllib2模块定义的函数和类用来获取URL(主要是HTTP的),他提供一些复杂的接口用于处理: 基本认证,重定向,Cookies等。   urllib2支持许多的“URL schemes”(由URL中的“:”之前的字符串确定 - 例如“FTP”的URL方案如“ftp://python.org/”),且他还支持其相关的网络协议(如FTP,HTTP)。我们则重点关注HTTP。   在简单的情况下,我们会使用urllib2模块的最常用的方法urlopen。但只要打开HTTP URL时遇到错误或异常的情况下,就需要一些HTTP传输协议的知识。我们没有必要掌握 HTTP RFC2616

2.爬虫基础――urllib2模块

匿名 (未验证) 提交于 2019-12-03 00:22:01
urllib2的使用 demo01.py # -*- coding:utf-8 -*- # 添加指定编码的注释,表示当前源代码支持中文操作 import urllib2 # 引入需要的模块 response = urllib2.urlopen("http://www.baidu.com") # 通过 urlopen() 方法请求百度数据,获取到的响应数据保存在 response 中 print(response.read()) # 打印展示获取到的数据 运行程序:python2 demo01.py 上述程序总分为两部分请求和相应 构造请求:request = urllib2.Request('http://www.baidu.com') 发送请求获得响应:response =urllib2.urlopen(request) 打印响应内容: print(response.read()) --------------------------------------------------------------------------------------------------------- 相应爬虫措施:请求头伪造,模仿浏览器发送请求; 程序如下: # 添加注释,源代码支持中文 # -*- coding:utf-8 -*- # 引入需要的模块 from urllib2

爬虫介绍

匿名 (未验证) 提交于 2019-12-03 00:08:02
Ŀ¼ 网络爬虫 (又被称为网页蜘蛛,网络机器人,在FOAF社区中间,更经常的称为网页追逐者),是一种按照一定的规则,自动地抓取万维网信息的程序或者脚本。另外一些不常使用的名字还有蚂蚁、自动索引、模拟程序或者蠕虫。 数据来源 企业产生的用户数据:百度指数、阿里指数、TBI腾讯浏览指数、新浪微博指数 数据平台购买数据:数据堂、国云数据市场、贵阳大数据交易所 政府/机构公开的数据:中华人民共和国国家统计局数据、世界银行公开数据、联合国数据、纳斯达克。 数据管理咨询公司:麦肯锡、埃森哲、艾瑞咨询 爬取网络数据:如果需要的数据市场上没有,或者不愿意购买,那么可以选择招/做一名爬虫工程师,自己动手丰衣足食。 学习内容 Python基础语法学习(基础知识) HTML页面的内容抓取(数据抓取) HTML页面的数据提取(数据清洗) Scrapy框架以及scrapy-redis分布式策略(第三方框架) 爬虫(Spider)、反爬虫(Anti-Spider)、反反爬虫(Anti-Anti-Spider)之间的斗争.... 根据使用场景,网络爬虫可分为 通用爬虫 和 聚焦爬虫 两种. 通用爬虫 通用搜索引擎(Search Engine)工作原理 通用网络爬虫 从互联网中搜集网页,采集信息,这些网页信息用于为搜索引擎建立索引从而提供支持,它决定着整个引擎系统的内容是否丰富,信息是否即时

Python urllib2 URLError HTTP status code.

六月ゝ 毕业季﹏ 提交于 2019-12-02 23:30:41
I want to grab the HTTP status code once it raises a URLError exception: I tried this but didn't help: except URLError, e: logger.warning( 'It seems like the server is down. Code:' + str(e.code) ) You shouldn't check for a status code after catching URLError , since that exception can be raised in situations where there's no HTTP status code available, for example when you're getting connection refused errors. Use HTTPError to check for HTTP specific errors, and then use URLError to check for other problems: try: urllib2.urlopen(url) except urllib2.HTTPError, e: print e.code except urllib2

python3报错:No module named 'urllib2'

匿名 (未验证) 提交于 2019-12-02 22:54:36
Python 3.3之后,urllib2改为urllib.response Python 2.7 代码: import urllib2 response = urllib2.urlopen( 'http://www.baidu.com/' ) html = response.read() print html Python 3.3 代码应该为: import urllib .request response=urllib .request .urlopen ( 'http://www.baidu.com' ) html=response.read() print (html) 文章来源: python3报错:No module named 'urllib2'

【python】【Memory leak】urllib2, request内存泄露问题解决方案

匿名 (未验证) 提交于 2019-12-02 22:51:30
提到python,很多人的第一直觉大概就是爬虫和网络相关。然而最近使用python2.7 urllib2和request的时候却无意中发现可能存在严重的内存泄漏问题,或者说垃圾回收有问题。stackoverflow了一下,确实有很多人反应了相关的问题,至今还没解决。综合了各种解决方案,最终确定了一种临时的替代方案,在此记录和分享。 用过python进行大量网络请求的童鞋,可能都会发现,当请求达到一定量之后会出现内存问题,无法再进行请求。下面,我们用利用python内置的gc库看一下urllib2和request请求过后的内存回收情况。 import gc, urllib, urllib2, requests def get_unreachable_memory_len () : # check memory on memory leaks gc.set_debug(gc.DEBUG_SAVEALL) gc.collect() unreachableL = [] for it in gc.garbage: unreachableL.append(it) print str(unreachableL) return len(str(unreachableL)) def request_with_urllib2 (url) : resp = urllib2.urlopen(url)