def urlrequsets(url, form = None,headers=None):
user_agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) 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''
print(555)
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)
# print(999)
# print(response)
html_byte = response.read().decode('utf-8')
except HTTPError as h:
print(h)
except URLError as u:
print(u)
except:
# print(22222)
return html_byte
if __name__ == '__main__':
url = 'http://fanyi.baidu.com'
form = {
'kw' :'你好'
}
# print(111)
html_byte = urlrequsets(url)
# html_byte = json.loads(html_byte)
print(html_byte)
爬虫小案例 GET & POST
最新推荐文章于 2024-03-27 11:10:01 发布