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/60750355html