一、get传参
1.汉字报错
解释器ascii没有汉字,需要url汉字转码
urllib.parse.qoute safe="string.printable"
2.字典传参
例如
#get带有字典获取
import urllib.request
import urllib.parse
import string
def get_params():
url = "http://www.baidu.com/s?"
#字典
params={
"wd":"中文",
"key":"zhang",
"value":"san"
}
#字典传参
str_params = urllib.parse.urlencode(params)
print(str_params)
final_url = url + str_params
#将带有中文的url 转译成计算机可以识别的url
end_url = urllib.parse.quote(final_url,safe=string.printable)
response = urllib.request.urlopen(end_url)
data = response.read().decode("utf-8")
print(data)
get_params()
3.User-Agent
(1)模拟真实的浏览器发送请求
(2)request.add_header(动态的添加head数据)
(3)响应头 response.header
(4)创建request:urlib.request.Request(url)
拼接URL地址的三种方式
1.字符串相加
'https://www.baidu.com/s? '+ urlencode({'wd':''美女,‘pn’:'50'})
2.字符串格式化(占位符)
'https://www.baidu.com/s?%s '+ % urlencode({'wd':''美女},‘pn’:'50'})
3.format()方法
'https://www.baidu.com/s?{} '.format(urlencode({'wd':''美女},‘pn’:'50'}))
例如
【1】request-header
import urllib.request
def load_baidu():
url="http://www.baidu.com"
#创建请求对象
request=urllib.request.Request(url)
#请求网络数据
response=urllib.request.urlopen(request)
print(response)
#二进制的mac电脑默认解码方式是utf-8
windows的默认方式是gbk
data=response.read().decode("utf-8")
#响应头
#print(response.headers)
#获取请求头的信息
request_headers=request.headers
print(request_hea