# coding=utf-8 import requests from retrying import retry ''' A method to specifically request a url address ''' 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) # Repeatedly execute 3 times, 3 times of reading an error will report an error, and once no error is reported, no error will be reported 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))