GET和POST的封裝
阿新 • • 發佈:2018-08-14
try 呵呵 utf-8 () return baidu error print ade
from urllib import request, parse from urllib.error import HTTPError, URLError def get(url, headers=None): return urlrequests(url, headers = headers) def post(url,form, headers=None): return urlrequests(url, form, headers=headers) def urlrequests(url, form=None,headers=None): user_agent= ‘Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36‘ #如果用戶需要自行傳入
if headers == None:
headers = {
‘User-Agent‘: user_agent
}
html_bytes = b‘‘
try:
if form:
#POST
#2.1轉換成str
form_str = parse.urlencode(form)
#2.2轉換成butes
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_byres = response.read()
except HTTPError as e:
print(e)
except URLError as e:
print(e)
return html_bytes
if __name__ == ‘__main__‘:
url = ‘http://fanyi.baidu.com/sug‘
form = {
‘kw‘: ‘呵呵‘
}
html_bytes = post(url, form=form)
print(html_bytes)
#url = ‘http://www.baidu.com‘
#html_byte = get(url)
#print(html_byte)
GET和POST的封裝