2021-01-05

理解Requests库的异常
requests. ConnectionError网络连接错误异常
requests. HTTPError HTTP错误异常
requests. URLRequired URL缺失异常
requests. TooManyRedirects 超过最大重定向次数异常
requests. ConnecrTimeout 连接远程服务器超时异常
requests. Timeout 请求URL超时异常
理解Reponse库的异常
r. raise_for_status()
如果不是200,产生异常requests. HTTPError
爬取网页的基本框架
def getHTMLText(url):
try:
r=requests. get(url,timeout=30)
r. raise_for_status()
r. encoding=r. apparent_encoding
return r. text
except:
return “产生异常”
if__main__==“main”:
url=""
print(getHTMLText(url))

你可能感兴趣的:(笔记)