1 import requests 2 3 def get_html_text(url): 4 try: 5 r = requests.get(url, timeout=30) 6 r.raise_for_status() # 如果状态不是200,引发HTTPError 异常 7 r.encoding = r.apparent_encoding 8 return r.text 9 except:10 return "产生异常"11 12 if __name__ == "__main__":13 url = "http://www.baidu.com"14 print(get_html_text(url))
http://blog.csdn.net/linzch3/article/details/60750355