urllib库使用方法1 request

urllib是可以模仿浏览器发送请求的库,Python自带

Python3中urllib分为:urllib.request和urllib.parse

import urllib.request

url ="http://www.baidu.com/"#必须要完整格式 - 带上协议类型
response = urllib.request.urlopen(url = url) #模拟浏览器向url发送请求,返回请求对象(响应内容)
print(response)#返回请求对象
print(response.geturl())#根据相应的内容,获取请求的url地址
print(response.getheaders())#获取头部信息,是列表,列表里面是元组
print(dict(response.getheaders()))#将获取的头部信息(列表中有元组值),转化为字典
print(response.getcode())#获取状态码

print(response.readlines())#readlines()是按行读取响应内容,是返回字节类型列表list
print(type(response.readlines())) #列表list都是字节类型 <class 'list'>

print(type(response.read()))#字节类型<class 'bytes'>
print(response.read())#获取到字节类型网页信息
print(response.read().decode())#解码后获取到字符串类型网页信息
print(type(response.read().decode()))#<class 'str'>

with open("baidu.html", "w", encoding="utf8") as fp:
fp.write(response.read().decode())

#直接以二进制形式写入
with open("baidu.html", "wb") as fp:
fp.write(response.read())#不用decode了
print(type(fp)) #<class '_io.BufferedWriter'>


#下载图片
image_url = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1550253981561&di=ef807465b42b2e19a572a98f0cfe8861&imgtype=0&src=http%3A%2F%2Fimages.enet.com.cn%2F2011%2F4%2F19%2F1304713401519.jpg"
#发送请求 响应内容response
response = urllib.request.urlopen(image_url)
#写入本地二进制的格式
with open("meizitu1.jpg", "wb") as fp:
fp.write(response.read())

#另种urlretrieve方式保存二进制格式图片
urllib.request.urlretrieve(image_url, "girl.jpg")





















猜你喜欢

转载自www.cnblogs.com/perfectdata/p/10387967.html
今日推荐