request的基本使⽤⽤法
(1)基本⽤法
url = "www.baidu/"
# 获取response对象
response = quest.urlopen(url)
# 读取内容 bytes类型
data = ad()
# 转化为字符串
str_data = data.decode("utf-8")
# 字符串转化为bytes
str_name = "baidu"
bytes_name =de("utf-8")
(2)url参数中含有汉字的需要转译
url = "www.baidu/s?wd="
name = "python中含有汉字"
final_url = url + name
#⽹址⾥⾯包含了汉字;ascii是没有汉字的;url转译
#将包含汉字的⽹址进⾏转译
encode_new_url = urllib.parse.quote(final_url,safe=string.printable)
# 使⽤代码发送⽹络请求
response = quest.urlopen(encode_new_url)
print(response)
#读取内容
data = ad().decode()
#保存到本地
with open("02-encode.html","w",encoding="utf-8")as f:
f.write(data)
(3)传⼊字典类型的参数
url = "www.baidu/s?"
params = {
"wd":"中⽂",
"key":"zhang",
"value":"san"
}
# 字典类型转译成参数
str_params = urllib.parse.urlencode(params)
final_url = url + str_params
# 将带有中⽂的url 转译成计算机可以识别的url
end_url = urllib.parse.quote(final_url,safe=string.printable)
response = quest.urlopen(end_url)
data = ad().decode("utf-8")
print(data)
(4)添加header
第⼀种添加header的⽅式
url = "www.baidu"
# 添加请求头的信息
headers = {
# 浏览器的版本
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36',
'name':'chen'
}
# 创建请求对象
request = quest.Request(url, headers=headers)
error parse new
第⼆种添加header的⽅式:动态添加
url = "www.baidu"
# 创建请求对象
request = quest.Request(url)
# 动态添加请求头信息
request.add_header("User-Agent",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36")
# 请求⽹络数据
response = quest.urlopen(request)
data = ad().decode("utf-8")
# 获取到完整的url
final_url = _full_url()
print(final_url)
with open("baidu.html", "w", encoding="utf-8") as f:
f.write(data)
# 获取请求头的信息
request_headers = request.headers
print(request_headers)
user_agent = _header('User-agent')
print(user_agent)
# 响应头
print(response.headers)
(5)使⽤代理
url = 'wwwblogs/chenshy'
# 添加代理
proxy = {
'http': '119.102.25.91:9999'
}
# 代理处理器
proxy_handler = quest.ProxyHandler(proxy)
# 创建⾃⼰的opener
opener = quest.build_opener(proxy_handler)
# 拿着代理ip发送请求
data = opener.open(url).read().decode("utf-8")
print(data)
(6) cookie
a.在头部添加cookie
url = 'www.yaozh/member/'
headers = {'User_Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36',
'Cookie':'acw_tc=707c9fd115550786016938465e492bb70702a65dacd78c0969a841171ddf8d; PHPSESSID=t4rb1af4vmks8gete5oqfd6ub7; _ga=GA1.2.521923122.1555078606; _gid=GA1.2.523976398.1555078606; Hm_lvt_65968db3ac1    request = quest.Request(url,headers=headers)
response = quest.urlopen(request)
data = ad().decode('utf-8')
print(data)
b.登录之后获取cookie ,cookiejar的使⽤
quest
from http import cookiejar
from urllib import parse
def login():
# 1.代码登录获取cookie 带着cookies请求个⼈中⼼
url = 'www.yaozh/login'
# 登录的参数
login_form_data = {
'username':  'lifelover',
'pwd': 'chen19960319',
'formhash': 'F456373F7B',
'backurl': 'https%3A%2F%2Fwww.yaozh%2F'
}
# 发送登录请求
cook_jar =  cookiejar.CookieJar()
cookie_handler = quest.HTTPCookieProcessor(cook_jar)
opener = quest.build_opener(cookie_handler)
# 带着参数,发送post请求
headers = {'User_Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36'}    # 1.需要转译,转码  2.需要byte类型
login_str = urllib.parse.urlencode(login_form_data).encode('utf-8')
request = quest.Request(url,headers=headers,data=login_str)
# 如果登录成功,cookiejar⾃动保存cookie
response = opener.open(request)
# 带着cookie请求个⼈中⼼
center = 'www.yaozh/member/'
center_request = quest.Request(center,headers=headers)
response = opener.open(center_request)
data = ad().decode('utf-8')
with open('test.html','w',encoding='utf-8') as f:
f.write(data)

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。