requests获取所有状态码

requests默认是不会获取301/302的状态码的。可以设置allow_redirects=False,这样就可以获取所有的状态码了

import requests

# url
# url = 'http://www.freebuf.com/news/157100.html' # 请求200,返回200
url = 'http://www.freebuf.com/fevents/133225.html' # 请求302,返回200。要想不跳转,获取302,用参数:allow_redirects=False
# url = 'http://www.freebuf.com/articles/database/151839.html' # 请求403,返回403
# url = 'http://www.freebuf.com/articles/database/1518391.html' # 请求存在的域名中不存在的页面,请求404,返回404
# url = 'http://www.freebudfsf.com/articles/database/1518391.html' # 请求不存在的域名。程序崩溃
# url = 'https://www.douban.com/group/topic/49606658/' # 请求存在的域名,公司限制访问,返回抛出异常,程序崩溃。效果和网络中断一样。
# url = 'http://10.1.75.241' # 请求ip,(一定要加协议HTTP,否则崩溃)
# headers
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'
}
try:
# 发请求,得响应
response = requests.get(url, headers=headers, allow_redirects=False)
# 解析
print(' give url:', url)
print(' request.url:', response.request.url)
print('response.url:', response.url)
print(response.content)
print(response.status_code)
except Exception as e:
print(e)

  

封装一个获取所有状态码的函数,同时实现验证返回值的方法

import requests

def get_statecode_or_errinfo(url=''):
'''
获取响应状态码,或者未响应的错误信息
:param url: 请求的url
:return: 状态码,或者未响应的错误信息
'''
if url == '':
return '请输入一个url作为get_statecode_or_errinfo的参数'
# headers
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'
}
try:
# 发请求,得响应
response = requests.get(url, headers=headers, allow_redirects=False)
# 返回状态码
return response.status_code
except Exception as e:
# 返回异常信息
return e if __name__ == '__main__':
# url
# url = 'http://www.freebuf.com/news/157100.html' # 请求200,返回200
url = 'http://www.freebuf.com/fevents/133225.html' # 请求302,返回200。要想不跳转,获取302,用参数:allow_redirects=False
# url = 'http://www.freebuf.com/articles/database/151839.html' # 请求403,返回403
# url = 'http://www.freebuf.com/articles/database/1518391.html' # 请求存在的域名中不存在的页面,请求404,返回404
# url = 'http://www.freebudfsf.com/articles/database/1518391.html' # 请求不存在的域名。程序崩溃。如果有Nginx,返回200
# url = 'http://dsfs' # 请求不存在的域名,设置了参数:allow_redirects=False,在有Nginx处理的情况下,有304,返回200。
# url = 'https://www.douban.com/group/topic/49606658/' # 请求存在的域名,公司限制访问,返回抛出异常,程序崩溃。效果和网络中断一样。
# url = 'http://10.1.75.241' # 请求ip,请求200,返回200(一定要加协议HTTP,否则崩溃) # url = 'http://www.freebuf.com/fevents/133225.html' # 请求302,返回200。要想不跳转,获取302,用参数:allow_redirects=False
url = 'http://www.freebuf.com/news/171238.html'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'
}
# response=requests.get(url,headers=headers,allow_redirects=False)
response=requests.get(url,headers=headers)
# 检查状态码
print(response.status_code) # # 检查url
print(url)
print(response.url)
#
# # 检查请求头
print(response.request.headers)
#
# # 检查响应头
print(response.headers)
#
# # 检查源码
# print(response.content)
# print(response.content.decode())
# print(response.text)
#
# response.encoding = 'utf-8'
# print(response.text)
print(response.encoding)
#
# # 检查源码字符串长度
print(len(response.content))

  

说明:

反扒:
总结多种验证返回值的方式。requests
比如:检查状态码、检查url(有可能发送了跳转)、检查请求头、检查响应头、检查源码、检查源码字符串长度。
检查状态码
print (response.status_code)
检查url
print (response.url)
检查请求头
print (response.request.headers)
检查响应头
print (response.headers)
检查源码字符串长度
print (len(response.content))
检查源码
print (response.content)
print (response.content.decode())
response.encoding='utf-8'
print (response.text)
print (response.encoding)

scrapy爬虫的响应规则:

# 1、被过滤掉,不发出请求:不在允许的域名范围内
# temp['title_url'] = "https://www.baidu.com/" # 跨域。请求发出前,url直接被过滤掉。
# temp['title_url'] = "http://open.freebuf.com/live?id=1021" # 跨域。请求发出前,url直接被过滤掉。
# temp['title_url'] = "http://10.1.75.241" # 请求ip地址,请求发出前,url直接过来掉。如果设置为允许ip网站,没有被过滤,就返回200 # 2、禁止访问
# temp['title_url'] = "http://www.freebuf.com/articles/database/151839.html"#禁止访问403,资源存在,不让访问。Ignoring non-200 response
# temp['title_url'] = "http://www.freebuf.com/articles/database/1518391.html"#禁止访问404,资源本身不存在。Ignoring non-200 response # 3、重定向后的作为新请求
# temp['title_url'] = "http://www.freebuf.com/news/156654.html" # 重定向301、302。会返回重定向后200的状态码 # 4、断网
# temp['title_url'] = "https://www.douban.com/group/topic/49606658/" # 公司限制访问。[<twisted.python.failure.Failure twisted.internet.error.ConnectionLost: Connection to the other side was lost in a non-clean fashion: Connection lost.>] # 5、没有的网站
# temp['title_url'] = "https://www.badfsdsdfsdfsdfsdddd.com/" # 直接被过滤掉,如果没有被过滤,就返回域名解析错误:DNS lookup failed: no results for hostname lookup: www.badfsdsdfsdfsdfsdddd.com.
pass

  

scrapy爬虫举例

freebuf2.py

# -*- coding: utf-8 -*-
import scrapy
from scrapy_FB.items import ScrapyFb2Item # from util.logger import Logger # logger_freebuf2 = Logger(logname=__name__, logpath='collection_log', logformat=1, loglevel=10).getlog()
# logger_freebuf2.debug('i am debug3')
# logger_freebuf2.info('i am info3')
# logger_freebuf2.warning('i am warning3') class Freebuf2Spider(scrapy.Spider):
# freebuf2爬虫
name = 'freebuf2'
allowed_domains = ['freebuf.com','douban.com']
start_urls = ['http://www.freebuf.com/page/708'] def parse(self, response):
cur_url = response.url # 当前列表页url
cur_page_num = int(cur_url.rpartition('/')[-1]) # 当前page num print('cur_url:%s' % cur_url)
print('cur_page_num:%s' % cur_page_num) # 获取列表节点
node_list = response.xpath('//*[@id="timeline"]/div/div[2]/dl/dt/a[1]')
print('len(node_list):%s' % len(node_list)) page_num = int(cur_url.rpartition('/')[-1]) # 当前页码
count_node = len(node_list) # 当前列表页,一共有的详细页条数 # 遍历节点
for i, node in enumerate(node_list):
# temp = {}
temp = ScrapyFb2Item()
temp['title'] = node.xpath('./text()').extract()[0].strip()
if i == 0:
# 1、被过滤掉,不发出请求:不在允许的域名范围内
# temp['title_url'] = "https://www.baidu.com/" # 跨域。请求发出前,url直接被过滤掉。
# temp['title_url'] = "http://open.freebuf.com/live?id=1021" # 跨域。请求发出前,url直接被过滤掉。
# temp['title_url'] = "http://10.1.75.241" # 请求ip地址,请求发出前,url直接过来掉。如果设置为允许ip网站,没有被过滤,就返回200 # 2、禁止访问
# temp['title_url'] = "http://www.freebuf.com/articles/database/151839.html"#禁止访问403,资源存在,不让访问。Ignoring non-200 response
# temp['title_url'] = "http://www.freebuf.com/articles/database/1518391.html"#禁止访问404,资源本身不存在。Ignoring non-200 response # 3、重定向后的作为新请求
# temp['title_url'] = "http://www.freebuf.com/news/156654.html" # 重定向301、302。会返回重定向后200的状态码 # 4、断网
# temp['title_url'] = "https://www.douban.com/group/topic/49606658/" # 公司限制访问。[<twisted.python.failure.Failure twisted.internet.error.ConnectionLost: Connection to the other side was lost in a non-clean fashion: Connection lost.>] # 5、没有的网站
# temp['title_url'] = "https://www.badfsdsdfsdfsdfsdddd.com/" # 直接被过滤掉,如果没有被过滤,就返回域名解析错误:DNS lookup failed: no results for hostname lookup: www.badfsdsdfsdfsdfsdddd.com.
pass else:
temp['title_url'] = node.xpath('./@href').extract()[0]
temp['page_num'] = str(page_num)
temp['line_num'] = i + 1
temp['line_total'] = str(count_node)
# print(temp['line_num'])
yield scrapy.Request(temp['title_url'], callback=self.parse_detail, meta={"meta_1": temp}, errback=self.err) if len(node_list) != 0: # 爬虫不终止的条件
# 下一页
next_url = 'http://www.freebuf.com/page/{}'.format(cur_page_num + 1)
# print('next_url:%s' % next_url)
yield scrapy.Request(next_url, callback=self.parse) # 访问下一页 def parse_detail(self, response):
item = response.meta['meta_1'] print(item['line_num'], item['title_url'])
# print(response.status)
print(item['line_num'], response.request.url) def err(self, response):
print('err:',response.request.url)
# print('err:',response.status)
# print(dir(response))
print('err:',response.getErrorMessage())
print(dir(response))
# print(type(response.getErrorMessage()))

  

requests获取所有状态码的更多相关文章

  1. 如何在使用 RemoteWebDriver 打开网页的同时获取 Http 状态码

    最近一直在用Selenium这个开源项目写一些web 自动化的小玩意.本来一直运行的挺好,直到有一天突然发现资源抓取失败了,翻看日志才发现,原来本该正常打开的页面返回了504错误所以自然失败了.如何避 ...

  2. LODOP获取打印机状态码和状态码含义测试

    由于打印机千差万别,打印机执行的标准也不一样,LODOP获取的打印状态码也可能不同,安装了个打印机驱动实际测试一下,测试的打印机驱动是Brother Color Type3 Class Driver. ...

  3. LODOP获取打印状态码和时间列表

    之前有博文介绍获取打印状态码和打印状态码的含义,相关博文:LODOP获取打印机状态码和状态码含义测试.此外 ,也有获取状态码及其变化的方法,可以获取打印状态码的列表,列表包含每个状态和每个状态的时间. ...

  4. 前端如何获取http状态码400的返回值

    axios.get("/check_mobile_and_sent_code",{withCredentials:true,params:{mobile:formInline.mo ...

  5. 【 总结 】crontab 使用脚本及直接获取HTTP状态码

    一.在crontab里面计划执行的脚本,所有的命令都要写出绝对路径.因为crontab的独立的进程,可能无法直接加载环境变量. 二.在判断网站能否正常访问一般的思路: 1. 判断网站是否能够正常打开. ...

  6. (六)获取http状态码和处理返回结果

    int StatusCode = httpResponse.getStatusLine().getStatusCode(); 处理返回结果: /** * 处理返回结果 * @param respons ...

  7. c# HttpWebResponse 各种情况下 获取StatusCode状态码

    捕捉网页出现404.500等会直接抛出WebException异常 异常代码: (HttpWebResponse)req.GetResponse(); 当执行这段代码出现异常 解决问题 那如果我们想获 ...

  8. (404) 未找到 获取StatusCode状态码

    异常代码: (HttpWebResponse)req.GetResponse(); 当执行这段代码出现异常 解决问题 那如果我们想获得错误发生时候服务器段错误页面的源代码该如何做呢? 其实非常非常简单 ...

  9. php获取响应状态码

    $ch = curl_init('http://www.jb51.net'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_exec($ch); ...

随机推荐

  1. 微信支付(公众号支付APIJS、app支付)服务端统一下单接口java版

    一.微信公众号支付APIJS: 要完整的实现微信支付功能,需要前后端一起实现,还需要微信商户平台的配置.这里只是涉及服务端的代码. jar包:pom.xml <!-- ↓↓↓↓↓↓↓↓ 支付相关 ...

  2. c++ 向main传递参赛

    1.今天写了个批处理文件 准备向main传递参赛  发现在vs2008运行正常 vs2010运行只有首字母 2.需要在项目--属性--常规-字符集里面设置成多字节就好了,以前是unicode 3.因为 ...

  3. Linux 权限修改

    chown -R 用户名:组名 文件夹名chown -R xu:hadoop hadoop 将hadoop目录(以及其下的所有子目录/文件)的属主用户设为xu, xu 的组名为group

  4. 第五章 面向方面编程___AOP入门

    上一篇讲了 AOP 和 OOP 的区别,这一次我们开始入门 AOP .实现面向方面编程的技术,主要分为两大类: 一是 采用动态代理技术,利用截取消息的方式,对该消息进行装饰,以取代原有对象行为的执行: ...

  5. 高级类特性----抽象类(abstract class)

    抽象类(abstract class) 随着继承层次中一个个新子类的定义,类变得越来越具体,而父类则更一般,更通用.类的设计应该保证父类和子类能够共享特征.有时将一个父类设计得非常抽象,以至于它没有具 ...

  6. python 协程(单线程中的异步调用)(转廖雪峰老师python教程)

    协程,又称微线程,纤程.英文名Coroutine. 协程的概念很早就提出来了,但直到最近几年才在某些语言(如Lua)中得到广泛应用. 子程序,或者称为函数,在所有语言中都是层级调用,比如A调用B,B在 ...

  7. On iPad, UIImagePickerController must be presented via UIPopoverController

    本文转载至:http://blog.csdn.net/k12104/article/details/8537695 On iPad, UIImagePickerController must be p ...

  8. python初学总结(二)

    (1)字典 字典是一种映射关系:键(key),值(value),key-value对 创建字典的方式:直接创建和利用dict函数创建 >>> aInfo = {'Wangdachui ...

  9. Mybatis——SQL语句构建器类

    SQL语句构建器类 问题 Java程序员面对的最痛苦的事情之一就是在Java代码中嵌入SQL语句.这么来做通常是由于SQL语句需要动态来生成-否则可以将它们放到外部文件或者存储过程中.正如你已经看到的 ...

  10. xcode7/ios9中 低版本app运行时,屏幕上下出现黑边的问题

    xcode从低版本升级至 7.0或更高版本后,某些低版本app再次编译运行后,发现app在设备上运行时,会在上端和底部 出现黑边的现象.这导致app的展示界面跟缩水了一样,变得十分丑陋. 对于这一问题 ...