urllib2

urllib,urllib2在python2与python3中的区别(转载)

懵懂的女人 提交于 2019-12-06 02:28:02
什么是Urllib库 Urllib是Python提供的一个用于操作URL的模块,我们爬取网页的时候,经常需要用到这个库。 升级合并后,模块中的包的位置变化的地方较多。在此,列举一些常见的位置变动,方便之前用Python2.x的朋友在使用Python3.x的时候可以快速掌握。 常见的变化有: 在Pytho2.x中使用import urllib2-------对应的,在Python3.x中会使用import urllib.request,urllib.error。 在Pytho2.x中使用import urllib-------对应的,在Python3.x中会使用import urllib.request,urllib.error,urllib.parse。 在Pytho2.x中使用import urlparse-------对应的,在Python3.x中会使用import urllib.parse。 在Pytho2.x中使用import urlopen-------对应的,在Python3.x中会使用import urllib.request.urlopen。 在Pytho2.x中使用import urlencode-------对应的,在Python3.x中会使用import urllib.parse.urlencode。 在Pytho2.x中使用import urllib

urllib

我是研究僧i 提交于 2019-12-06 00:38:25
About urllib是Python内置的HTTP请求库。urllib 模块提供的上层接口,使访问 www 和 ftp 上的数据就像访问本地文件一样,并且它也是requests的底层库。 其中包括4个主要模块: urllib.request:请求模块。 urllib.error:异常处理模块。 urllib.parse:URL解析模块。 urllib.robotparser:robots.txt解析模块。 这里以Python3.6为例。 urllib、urllib2、urllib3 在Python2.x中,分为urllib和urllib2,简单来说,urllib2是urllib的增强版,但urllib中的函数又比urllib2多一些,对于简单的下载之类的,urllib绰绰有余,如果涉及到实现HTTP身份验证或者cookie或者扩展编写自定义协议,urllib2更好一些。 urllib支持设置编码的函数urllib.urlencode,在模拟登陆的时候经常需要传递经过post编码之后的参数,如果不想使用第三方库完成模拟登录,就必须使用到标准库中的urllib。urllib提供一些比较原始基础的方法而urllib2并没有,比如urllib中的urlencode方法用来GET查询字符串的产生。 urllib2比较有优势的地方在于urllib2

urllib3 maxretryError

可紊 提交于 2019-12-06 00:24:52
问题 I have just started using urllib3, and I am running into a problem straightaway. According to their manuals, I started off with the simple example: Python 2.7.1+ (r271:86832, Apr 11 2011, 18:13:53) [GCC 4.5.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import urllib3 >>> >>> http = urllib3.PoolManager() >>> r = http.request('GET', 'http://google.com/') I get thrown the following error: Traceback (most recent call last): File "<stdin>", line 1, in

urllib2 urlopen works very randomly

人走茶凉 提交于 2019-12-06 00:14:13
For some reasons this part where I fetch JSON data from following url will only works sometimes. And sometimes it will return 404 error, and complain about missing header attribute. It will work 100% of the time if I paste it onto a web browser. So I'm sure the link is not broken or something. I get the following error in Python: AttributeError: 'HTTPError' object has no attribute 'header' What's the reason for this and can it be fixed? Btw I removed API key since it is private. try: url = "http://api.themoviedb.org/3/search/person?api_key=API-KEY&query=natalie+portman" header = { 'User-Agent'

python urllib2 实现大文件下载

怎甘沉沦 提交于 2019-12-05 22:21:27
使用urllib2下载并分块copy: # from urllib2 import urlopen # Python 2 from urllib.request import urlopen # Python 3 response = urlopen(url) CHUNK = 16 * 1024 with open(file, 'wb') as f: while True: chunk = response.read(CHUNK) if not chunk: break f.write(chunk) 另一种大文件copy方式, shutil: import shutil try: from urllib.request import urlopen # Python 3 except ImportError: from urllib2 import urlopen # Python 2 def get_large_file(url, file, length=16*1024): req = urlopen(url) with open(file, 'wb') as fp: shutil.copyfileobj(req, fp, length) 关于shutil的一些介绍: https://www.cnblogs.com/zhangboblogs/p/7821702.html

Python urllib2的使用点滴: cookie, https, put, 500/40...

独自空忆成欢 提交于 2019-12-05 20:30:42
最近用 urllib2 写了一个公司内部用的脚本 这个脚本要访问一个webservice,访问之前先要用https登陆拿到cookie再到另一个地方获取一个临时用的id https登陆,获取cookie 首先是https登陆,这段很好写,以前 写过校内网发帖机 ,轻车熟路,用cookielib的CookieJar加上HTTPCookieProcessor搞定,代码如下(其中那个超简单的lambda hack简直绝了: #! /usr/bin/env python # -*- coding: utf-8 -*- import urllib , urllib2 , sys , cookielib , re , os , json cj = cookielib . CookieJar () opener = urllib2 . build_opener ( urllib2 . HTTPCookieProcessor ( cj )) url_login = 'https://xxx.yahoo.com/login/' body = (( 'username' , '半瓶墨水' ), ( 'password' , '密码' ), ( 'action' , 'login' ),) print 'login to get cookies' opener . open ( url_login ,

Python爬虫入门:Cookie的使用

旧巷老猫 提交于 2019-12-05 20:30:31
大家好哈,上一节我们研究了一下爬虫的异常处理问题,那么接下来我们一起来看一下Cookie的使用。 为什么要使用Cookie呢? Cookie,指某些网站为了辨别用户身份、进行session跟踪而储存在用户本地终端上的数据(通常经过加密) 比如说有些网站需要登录后才能访问某个页面,在登录之前,你想抓取某个页面内容是不允许的。那么我们可以利用Urllib2库保存我们登录的Cookie,然后再抓取其他页面就达到目的了。 在此之前呢,我们必须先介绍一个opener的概念。 1.Opener 当你获取一个URL你使用一个opener(一个urllib2.OpenerDirector的实例)。在前面,我们都是使用的默认的opener,也就是urlopen。它是一个特殊的opener,可以理解成opener的一个特殊实例,传入的参数仅仅是url, data ,timeout。 如果我们需要用到Cookie,只用这个opener是不能达到目的的,所以我们需要创建更一般的opener来实现对Cookie的设置。 2.Cookielib cookielib模块的主要作用是提供可存储cookie的对象,以便于与urllib2模块配合使用来访问Internet资源。 Cookielib模块非常强大,我们可以利用本模块的CookieJar类的对象来捕获cookie并在后续连接请求时重新发送

HTTP: Proxy Authentification Error for nltk.download()

给你一囗甜甜゛ 提交于 2019-12-05 19:45:30
I am using nltk.download() to download the packages i need. But i am getting the following error. root@nishant-Inspiron-1545:/home/nishant/Dropbox/DDP/data# python Python 2.7.3 (default, Apr 10 2013, 05:09:49) [GCC 4.7.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import nltk >>> import nltk.downloader >>> nltk.download() NLTK Downloader --------------------------------------------------------------------------- d) Download l) List c) Config h) Help q) Quit --------------------------------------------------------------------------- Downloader> d

Fetch first n bytes from the URL

强颜欢笑 提交于 2019-12-05 18:51:46
Is that possible to fetch only a number of bytes from some URL and then close the connection with urllib/urllib2? Or even may be a part from n-th byte to k-th? There is a page on that side and I don't need to load the whole page, only a piece of it. You can set the Range header to request a certain range of bytes, but you are dependent on the server to honor the request: import urllib2 req = urllib2.Request('http://www.python.org/') # # Here we request that bytes 18000--19000 be downloaded. # The range is inclusive, and starts at 0. # req.headers['Range']='bytes=%s-%s' % (18000, 19000) f =

Basic fetching of a URL's HTML body with Python 3.x

冷暖自知 提交于 2019-12-05 18:32:55
I'm a Python newbie. I have been a little confused by the differences between the old urllib and urllib2 in Python 2.x and the new urllib in Python 3, and among other things I'm not sure when data needs to be encoded before being sent to urlopen. I have been trying to fetch the html body of a url, using a POST so that I can send parameters. The webpage displays sunshine data for a country over a particular hour of a given day. I have tried without encoding/decoding and the printout is a string of bytes with b at the beginning. The code I then tried was import urllib.request, urllib.parse,