raise_for_status()方法

raise_for_status()方法
理解Response类非常重要,Response这样的一个对象返回了所有的网页内容,那么它也提供了一个方法,叫raise_for_status(),这个方法是专门与异常打交道的方法,该方法有这样一个有趣的功能,它能够判断返回的Response类型状态是不是200。如果是200,他将表示返回的内容是正确的,如果不是200,他就会产生一个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 “产生异常”
这个代码中我们用r.raise_for_status()方法,它就可以有效的判断网络连接的状态。如果网连接出现错误,那么它就会用try-except来获取一个异常。而这个异常,在异常部分,我们用了一句 return “产生异常” 来表示,我们捕获到了这个异常,所以这样一个通用代码框架可以有效的处理,我们在访问或爬取网页过程中,它可能出现的一些错误,或者是网络不稳定造成的一些现象

  • 35
    点赞
  • 89
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
---------ChromeDriver 75.0.3770.140 (2019-07-12)--------- Supports Chrome version 75 Resolved issue 3007: ChromeDriver 75 - Running Javascript kills driver [Pri-1] Resolved issue 2943: goog:chromeOptions.w3c=false doesn't work for POST request with empty body [Pri-1] Resolved issue 2975: Webdriver Actions for keys (CONTROL, SHIFT etc..) not working in ChromeDriver 75.0.3770.8 [Pri-2] Resolved issue 2947: No W3C compliant endpoints for retrieving logs [Pri-2] Resolved issue 2622: Clear Element command raises blur event twice [Pri-2] Resolved issue 2803: Capability name loggingPrefs is not spec compliant [Pri-2] Resolved issue 2536: Make standards mode (goog:chromeOptions.w3c:true) the default [Pri-2] Resolved issue 2836: Error handling in W3C mode [Pri-2] Resolved issue 2414: Creating cookies without leading dot in the domain results in host-only cookies [Pri-2] Resolved issue 2796: ChromeDriver should allow null for script timeout [Pri-2] Resolved issue 2721: Element Send Keys to file input with 'multiple' attribute should append file list [Pri-3] Resolved issue 2850: Get Active Element should return "no such element" error when no active element exists [Pri-3] Resolved issue 2851: Navigate to malformed URL should raise "invalid argument" error [Pri-3] Resolved issue 2857: ChromeDriver returns wrong HTTP status code when invalid session id is received [Pri-3] Resolved issue 2719: When input parameter is missing or malformed, ChromeDriver response is not spec compliant [Pri-3] Resolved issue 2746: Execute Script does not handle line-oriented JavaScript comment [Pri-3]
this is a book about python. it was written by Swaroop C H.its name is "a byte of python". Table of Contents Preface Who This Book Is For History Lesson Status of the book Official Website License Terms Using the interpreter prompt Choosing an Editor Using a Source File Output How It Works Executable Python programs Getting Help Summary 4. The Basics Literal Constants Numbers Strings Variables Identifier Naming Data Types Objects Output How It Works Logical and Physical Lines Indentation Summary 5. Operators and Expressions Introduction Operators Operator Precedence Order of Evaluation Associativity Expressions Using Expressions Summary 6. Control Flow Introduction The if statement ivUsing the if statement How It Works The while statement Using the while statement The for loop Using the for statement Using the break statement The continue statement Using the continue statement Summary 7. Functions Introduction Defining a Function Function Parameters Using Function Parameters Local Variables Using Local Variables Using the global statement Default Argument Values Using Default Argument Values Keyword Arguments Using Keyword Arguments The return statement Using the literal statement DocStrings Using DocStrings Summary 8. Modules Introduction Using the sys module Byte-compiled .pyc files The from..import statement A module's __name__ Using a module's __name__ Making your own Modules Creating your own Modules from..import The dir() function Using the dir function Summary 9. Data Structures Introduction List Quick introduction to Objects and Classes Using Lists Tuple Using Tuples Tuples and the print statement Dictionary Using Dictionaries Sequences Using Sequences References Objects and References More about Strings String Methods Summary 10. Problem Solving - Writing a Python Script The Problem The Solution First Version Second Version Third Version Fourth Version More Refinements The Software Development Process Summary 11. Object-Oriented Programming Introduction The self Classes Creating a Class object Methods Using Object Methds The __init__ method Using the __init__ method Class and Object Variables Using Class and Object Variables Inheritance Using Inheritance Summary 12. Input/Output Files Using file Pickle Pickling and Unpickling Summary 13. Exceptions Errors Try..Except Handling Exceptions Raising Exceptions How To Raise Exceptions Try..Finally Using Finally Summary 14. The Python Standard Library Introduction The sys module Command Line Arguments More sys The os module Summary 15. More Python Special Methods Single Statement Blocks List Comprehension Using List Comprehensions Receiving Tuples and Lists in Functions Lambda Forms Using Lambda Forms The exec and eval statements The assert statement The repr function Summary 16. What Next? Graphical Software Summary of GUI Tools Explore More Summary A. Free/Libré and Open Source Software (FLOSS) B. About Colophon About the Author C. Revision History Timestamp
---------ChromeDriver 75.0.3770.140 (2019-07-12)--------- Supports Chrome version 75 Resolved issue 3007: ChromeDriver 75 - Running Javascript kills driver [Pri-1] Resolved issue 2943: goog:chromeOptions.w3c=false doesn't work for POST request with empty body [Pri-1] Resolved issue 2975: Webdriver Actions for keys (CONTROL, SHIFT etc..) not working in ChromeDriver 75.0.3770.8 [Pri-2] Resolved issue 2947: No W3C compliant endpoints for retrieving logs [Pri-2] Resolved issue 2622: Clear Element command raises blur event twice [Pri-2] Resolved issue 2803: Capability name loggingPrefs is not spec compliant [Pri-2] Resolved issue 2536: Make standards mode (goog:chromeOptions.w3c:true) the default [Pri-2] Resolved issue 2836: Error handling in W3C mode [Pri-2] Resolved issue 2414: Creating cookies without leading dot in the domain results in host-only cookies [Pri-2] Resolved issue 2796: ChromeDriver should allow null for script timeout [Pri-2] Resolved issue 2721: Element Send Keys to file input with 'multiple' attribute should append file list [Pri-3] Resolved issue 2850: Get Active Element should return "no such element" error when no active element exists [Pri-3] Resolved issue 2851: Navigate to malformed URL should raise "invalid argument" error [Pri-3] Resolved issue 2857: ChromeDriver returns wrong HTTP status code when invalid session id is received [Pri-3] Resolved issue 2719: When input parameter is missing or malformed, ChromeDriver response is not spec compliant [Pri-3] Resolved issue 2746: Execute Script does not handle line-oriented JavaScript comment [Pri-3]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值