from urllib import request,parse
from urllib.error import HTTPError,URLError
# get请求函数
def get(url,headers=None):
return urlrequest(url,headers=headers)
# post请求函数
def post(url,form,headers=None):
return urlrequest(url,form,headers=headers)
def urlrequest(url,form=None,headers=None):
user_agent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.84 Safari/537.36'
if headers == None:
headers = {
"User-Agent": user_agent
}
html_byte = b''
# 异常处理
try:
if form:
# POST请求
# 转换成str
form_str = parse.urlencode(form)
# 转换成bytes
form_bytes = form_str.encode('utf-8')
req = request.Request(url, data=form_bytes, headers=headers)
else:
# get请求
req = request.Request(url, headers=headers)
# 发送请求
response = request.urlopen(req)
html_byte = response.read()
except HTTPError as e:
print(e)
except URLError as e:
print(e)
return html_byte
if __name__ == "__main__":
url = "http://fanyi.baidu.com/sug"
data = {
'kw': '呵呵'
}
html_byte = post(url,form=data)
print(html_byte)
# url = "http://www.baidu.com"
# html_byte = get(url)
# print(html_byte.decode('utf-8'))
爬虫一:get和post函数封装
最新推荐文章于 2023-06-26 22:59:43 发布