pythonurllib2没有了
1. python的httplib,urllib和urllib2的区别及用
urllib和urllib2
urllib 和urllib2都是接受URL请求的相关模块,但是urllib2可以接受一个Request类的实例来设置URL请求的headers,urllib仅可以接受URL。
这意味着,你不可以伪装你的User Agent字符串等。
urllib提供urlencode方法用来GET查询字符串的产生,而urllib2没有。这是为何urllib常和urllib2一起使用的原因。
目前的大部分http请求都是通过urllib2来访问的
httplib
httplib实现了HTTP和HTTPS的客户端协议,一般不直接使用,在python更高层的封装模块中(urllib,urllib2)使用了它的http实现。
2. 为什么我下载的Python3.6,urllib包里面没有urlopen方法
Python3.x以上版本里的urllib模块已经发生改变,此处的urllib都应该改成urllib.request。
例如要写成这样:
import urllib.request
web = urllib.request.urlopen('http://www..com')
f = web.read()
print(f)
3. python3中使用urllib进行https请求
刚入门python学习网络爬虫基础,我使用的python版本是python3.6.4,学习的教程参考 Python爬虫入门教程
python3.6的版本已经没有urllib2这个库了,所以我也不需要纠结urllib和urllib2的区别和应用场景
参考这篇官方文档 HOWTO Fetch Internet Resources Using The urllib Package 。关于http(s)请求一般就get和post两种方式较为常用,所以写了以下两个小demo,url链接随便找的,具体场景具体变化,可参考注释中的基本思路
POST请求:
GET请求:
注意,
使用ssl创建未经验证的上下文,在urlopen中需传入上下文参数
urllib.request.urlopen(full_url, context=context)
这是Python 升级到 2.7.9 之后引入的一个新特性,所以在使用urlopen打开https链接会遇到如下报错:
ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:777)
所以,当使用urllib.urlopen打开一个 https 链接时,需要先验证一次 SSL 证书
context = ssl._create_unverified_context()
或者或者导入ssl时关闭证书验证
ssl._create_default_https_context =ssl._create_unverified_context
4. python3.4没有urllib2怎么办
python 3.x中urllib库和urilib2库合并成了urllib库。
其中urllib2.urlopen()变成了urllib.request.urlopen()
urllib2.Request()变成了urllib.request.Request()
5. python3.4中urllib 有没有urlencode函数
python3.x中urlencode在urllib.parse模块中
使用方式urllib.parse.urlencode
urllib.parse.urlencode(query,
doseq=False, safe='', encoding=None,
errors=None, quote_via=quote_plus)
Convert a mapping object or a sequence of two-element tuples, which may
contain str
or bytes objects, to a “percent-encoded” string. If the
resultant string is to be used as a data for POST operation with urlopen() function, then it should be properly
encoded to bytes, otherwise it would result in a TypeError.
The resulting string is a series of key=value pairs separated by '&' characters, where
both key and value are quoted using the quote_via
function. By default, quote_plus() is used to quote the values, which
means spaces are quoted as a '+' character and ‘/’ characters are encoded as %2F, which follows the
standard for GET requests (application/x-www-form-urlencoded). An alternate
function that can be passed as quote_via is quote(), which will encode spaces as %20 and not encode ‘/’
characters. For maximum control of what is quoted, use quote and specify a value
for safe.
When a sequence of two-element tuples is used as the query argument,
the first element of each tuple is a key and the second is a value. The value
element in itself can be a sequence and in that case, if the optional parameter
doseq is evaluates to True, indivial key=value pairs separated
by '&' are
generated for each element of the value sequence for the key. The order of
parameters in the encoded string will match the order of parameter tuples in the
sequence.
The safe, encoding, and errors parameters are
passed down to quote_via (the encoding and errors
parameters are only passed when a query element is a str).
To reverse this encoding process, parse_qs() and parse_qsl() are provided in this mole to parse
query strings into Python data structures.
Refer to urllib examples to
find out how urlencode method can be used for generating query string for a URL
or data for POST.
Changed in version 3.2: Query parameter
supports bytes and string objects.
6. python的httplib,urllib和urllib2的区别及用
整体来说,urllib2是urllib的增强,但是urllib中有urllib2中所没有的函数。 urllib2可以用urllib2.openurl中设置Request参数,来修改Header头。如果你访问一个网站,想更改User Agent(可以伪装你的浏览器),你就要用urllib2. urllib支持设置