requests库的异常处理
作者:互联网
import requests def getHTMLText(url): try: r = requests.get(url, timeout = 30) r.raise_for_status() # 利用apparent_encoding替代encoding # 保证返回内容的解码是正确的 r.encoding = r.apparent_encoding # 返回网页的内容 return r.text except Exception as e: # 直接输出异常具体信息 print(e) # return "产生异常" if __name__ == '__main__': url = 'http://baidu.com' print(getHTMLText(url))
标签:__,return,encoding,处理,url,getHTMLText,requests,异常 来源: https://blog.csdn.net/sy20173081277/article/details/114530700