# coding=utf-8 import requests from retrying import retry ''' 专门请求url地址的方法 ''' headers = {"User-Agent": "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Mobile Safari/537.36"} @retry(stop_max_attempt_number=3) # 重复执行3次,3次读报错才报错,有一次不报错就不报错 def _parse_url(url): response = requests.get(url,headers=headers,timeout=5) # 超时时间5秒 return response.content.decode() def parse_url(url): try: html_str =_parse_url(url) except: html_str = None return html_str if __name__ =='__main__': url = "http://www.baidu.com/" print(parse_url(url))
python requests 封装请求
猜你喜欢
转载自www.cnblogs.com/zqrios/p/8994116.html
今日推荐
周排行