python__爬虫自学异常 (二)
来源:互联网 发布:淘宝网店从哪里注册 编辑:程序博客网 时间:2024/06/07 04:03
requests库中的异常
requests.ConnectionError 网络连接异常
requests.HTTPError Http错误异常
requests.URLRequired URL缺失异常
reuqests.TooManyRedirects 超过最大重定向次数,产生重定向异常
requests.ConnectTimeout 连接远程服务器异常
requests.Timeout 请求超时产生的异常
r. raise_for_status() 如果返回值不是200,产生requests.HTTPErro
通常用的代码框架:
import requestsdef getHTTMText(url): try: r=requests.get(url,timeout=30)#请求连接,30s r.raise_for_status() #如果状态不是200,一发HttpErro异常,raise增加 r.encoding = r.apparent_encoding #自解码 return r.text #返回页面的响应 except: return"产生异常"if __name__=="__main__": url="http://www.baidu.com" text= getHTTMText(url) print(text)
阅读全文
0 0
- python__爬虫自学异常 (二)
- python__异常
- python__博客网络爬虫作业回顾
- python写爬虫技巧(二):异常处理
- Python爬虫包 BeautifulSoup 学习(二) 异常处理
- oracle自学(二)
- vbs自学(二)
- ActiveMq 自学(二)
- JAVA自学(二)
- python爬虫自学笔记本(2015.12.14)
- 自学Python之Scrapy爬虫:(一)爬虫基础
- python 爬虫(二)
- 爬虫学习(二)
- golang 爬虫(二)
- python爬虫(二)
- python爬虫(二)
- Python 爬虫(二)
- 爬虫(二)
- Android设计模式
- malloc\colloc
- 1002. 写出这个数 (20)
- Shell echo命令
- shadowsocks后端一键脚本
- python__爬虫自学异常 (二)
- 读取MP3内嵌信息
- 10065---【Java多线程】线程池的工作原理详解(上)
- K-近邻算法
- POJ 2422 The Wolves and the Sheep 笔记
- IntelliJ IDEA安装、配置、测试(win7_64bit)
- 机器学习--神经网络算法系列--交叉熵(Cross_entropy)
- hashMap的原理 深入理解
- React Native网络请求fetch简单封装