python中urllib模块和requests模块使用方法

import urllib.request

url = 'http://www.baidu.com'
# 进行请求
response = urllib.request.urlopen(url)
# print(response)
# 获取状态码
print(response.getcode())
# 获取URL
print(response.geturl())
# 获取请求头
print(response.getheaders())
# 读取响应
print(response.read().decode('UTF-8'))
# 下载数据 保存文件名称为baidu.html
urllib.request.urlretrieve(url, filename='baidu.html')
url转码: 
import urllib.request
url = 'https://www.baidu.com/s?wd=%E8%BF%AA%E4%B8%BD%E7%83%AD%E5%B7%B4'
# url转码
# print(urllib.request.unquote(url))
# 输出结果:https://www.baidu.com/s?wd=迪丽热巴
# url = urllib.request.unquote(url)
# print(url)
headers = {
    'User-Agent':' Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36'
}
# 构造请求对象,即模仿浏览器
request = urllib.request.Request(url, headers=headers)

你可能感兴趣的:(python,python,开发语言)