(二)网络请求:urllib库和requests库的使用
程序员文章站
2022-05-09 21:16:39
...
一、urllib库
python3内置的urllib库不需要安装直接import导入
该库中主要模块有
模块 | 含义 | 功能 |
---|---|---|
urllib.request | 请求模块 | 打开和读取URL |
urllib.parse | 解析模块 | 解析和处理URL |
urllib.response | 响应模块 | |
urllib.error | 异常处理模块 | 包含了urllib.request产生的异常 |
urllib.robotparse | 解析页面的robots.txt |
>>> help(urllib)
Help on package urllib:
NAME
urllib
PACKAGE CONTENTS
error
parse
request
response
robotparser
FILE
d:\360downloads\python\lib\urllib\__init__.py
(1)urllib.requset
>>> import urllib
>>> help(urllib.urlopen)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'urllib' has no attribute 'urlopen'
>>> help(urllib.request.urlopen)
Help on function urlopen in module urllib.request:
所有和网络请求有关的方法都集到了urllib库下面的request模块
注意直接import urllib不能访问到urllib.urlopen
应该是import urllib.request 或from urllib import request
(1)使用内置urllib库实现get简单实例:urlopen()函数
urlopen函数定义
urlopen(url, data=None, [timeout, ]*, cafile=None, capath=None, cadefault=False, context=None)
url:需要打开的网址
data:Post提交的数据,没有这个参数时实现的是get方法/get请求不需要传入data参数
timeout:设置网站的访问超时时间
urllib.request.urlopen()返回'http.client.HTTPResponse'对象
可通过该对象的方法获取某些数据信息和响应信息如
(1)f.read(size)/f.readline()/f.readlines()
读取的数据是字节型bytes
可调用bytes.decode()方法进行解码转成str类型 如data.decode('utf-8')
bytes对象常用方法见下文
(2)f.geturl()/f.url 获取请求的url #https://www.baidu.com
(3)f.getcode() 获取本次请求的HTTP响应状态码 #200
(4)f.getheaders():表示远程服务器返回的头部全部信息,
以列表形式返回,每一个列表元素又是一个有两个元素的元组,分别构成key-value
(5)f.getheader(name):获取响应报头中name字段的值
(6)f.info():以字典形式返回HTTPMessage对象,表示远程服务器返回的头信息
(7)f.fileno()/f.close() :对HTTPResponse类型数据进行操作
具体可通过dir(object),help(object)查看其方法、属性和函数定义
import urllib.request
# 构建GET请求
f = urllib.request.urlopen("https://www.baidu.com")
data=f.read()
print(data.decode('utf-8'))
>>> type(f)
<class 'http.client.HTTPResponse'>
>>>type(data)
<class 'bytes'>
byte对象常用方法 更多参考help(bytes)/dir(bytes)
class bytes(object)
| bytes(iterable_of_ints) -> bytes
| bytes(string, encoding[, errors]) -> bytes
将str对象按照指定编码方式encoding转成bytes对象
这在下文用urlopen()实现post会用到
| bytes(bytes_or_buffer) -> immutable copy of bytes_or_buffer
| bytes(int) -> bytes object of size given by the parameter
initialized with null bytes
(2)两种方法使用内置urllib库实现post的简单实例:urlopen()函数
方法一:urllib.request.urlopen()
实现Post,urlopen()的data参数必须是bytes或者iterable of bytes,不能是str
data参数封装的两种情况
如果是str型数据
进行bytes.encode(str,encoding)编码
如果是字典或恰含有两个元素的元组的数据
通过urllib.parse.urlencode(data)转成str类型,
再传入bytes.encode(str,encoding)方法
urlencode()方法详解见后文urllib.parse模块
import urllib.request
import urllib.parse
# 需要传入的data内容
data = {
"name": "python"
}
# 将data进行转码
data = bytes(urllib.parse.urlencode(data),encoding='utf-8')
#构建POST请求方法一
reponse = urllib.request.urlopen("http://www.baidu.com",data = data)
print(reponse.read())
方法二:urllib.request.Request()
类定义
class Request(builtins.object)
| Request(url, data=None, headers={}, origin_req_host=None,
unverifiable=False, method=None)
|
| Methods defined here:
|
| __init__(self, url, data=None, headers={}, origin_req_host=None,
unverifiable=False, method=None)
|
| add_header(self, key, val)
|
| add_unredirected_header(self, key, val)
|
| get_full_url(self)
|
| get_header(self, header_name, default=None)
|
| get_method(self)
| Return a string indicating the HTTP request method.
|
| has_header(self, header_name)
|
| has_proxy(self)
|
| header_items(self)
|
| remove_header(self, header_name)
|
| set_proxy(self, host, type)
|
#构建POST请求方法二
req = urllib.request.Request(url='http://httpbin.org/post',\
data=b'Datawhale') # 构建请求对象
with urllib.request.urlopen(req) as f:
data=f.read()
print(data.decode('utf-8'))
上一篇: scrapy爬取汽车之家图片之pipeline方法进阶
下一篇: 数据提取(二):xpath - lxml从字符串和文件中解析html代码 etree.html(),etree.tostring(),etree.parse(),etree.HTMLParser()
推荐阅读
-
使用php的HTTP请求的库Requests实现美女图片墙
-
python利用requests库模拟post请求时json的使用教程
-
使用Ajax和Jquery配合数据库实现下拉框的二级联动的示例
-
Python3爬虫(四)请求库的使用requests
-
Python2和Python3中urllib库中urlencode的使用注意事项
-
使用urllib库的urlretrieve()方法下载网络文件到本地的方法
-
python利用requests库模拟post请求时json的使用
-
使用php的HTTP请求的库Requests实现美女图片墙_PHP
-
使用php的HTTP请求的库Requests实现美女图片墙_PHP
-
使用php的HTTP请求的库Requests实现美女图片墙,