如何进行Request的分析

发布时间:2021-12-04 15:10:25 作者:柒染
来源:亿速云 阅读:185

如何进行Request的分析,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

介绍

Request类是一个http请求的类,对于爬虫而言是一个很重要的类。通常在Spider中创建这样的一个请求,在Downloader中执行这样的一个请求。同时也有一个子类FormRequest继承于它,用于post请求。

在Spider中通常用法: 

yield scrapy.Request(url = 'zarten.com')

类属性和方法有:

url  method  headers  body  meta  copy()  replace([url, method, headers, body, cookies, meta, encoding, dont_filter, callback, errback])

Request 

class scrapy.http.Request(url[, callback, method='GET', headers, body, cookies, meta, encoding='utf-8', priority=0, dont_filter=False, errback, flags])

参数说明:

cookies = {'name1' : 'value1' , 'name2' : 'value2'}

list方式: 

cookies = [     {'name': 'Zarten', 'value': 'my name is Zarten', 'domain': 'example.com', 'path': '/currency'}     ]
from scrapy.spidermiddlewares.httperror import HttpError     from twisted.internet.error import DNSLookupError     from twisted.internet.error import TimeoutError, TCPTimedOutError     class ToScrapeCSSSpider(scrapy.Spider):         name = "toscrape-css"         # start_urls = [         #     'http://quotes.toscrape.com/',         # ]         start_urls = [             "http://www.httpbin.org/",  # HTTP 200 expected             "http://www.httpbin.org/status/404",  # Not found error             "http://www.httpbin.org/status/500",  # server issue             "http://www.httpbin.org:12345/",  # non-responding host, timeout expected             "http://www.httphttpbinbin.org/",  # DNS error expected         ]         def start_requests(self):             for u in self.start_urls:                 yield scrapy.Request(u, callback=self.parse_httpbin,                                      errback=self.errback_httpbin,                                      dont_filter=True)         def parse_httpbin(self, response):             self.logger.info('Got successful response from {}'.format(response.url))             # do something useful here...         def errback_httpbin(self, failure):             # log all failures             self.logger.info(repr(failure))             # in case you want to do something special for some errors,             # you may need the failure's type:             if failure.check(HttpError):                 # these exceptions come from HttpError spider middleware                 # you can get the non-200 response                 response = failure.value.response                 self.logger.info('HttpError错误 on %s', response.url)             elif failure.check(DNSLookupError):                 # this is the original request                 request = failure.request                 self.logger.info('DNSLookupError错误 on %s', request.url)             elif failure.check(TimeoutError, TCPTimedOutError):                 request = failure.request                 self.logger.info('TimeoutError错误 on %s', request.url)
yield scrapy.Request(url = 'zarten.com', meta = {'name' : 'Zarten'})

在Response中:

my_name = response.meta['name']

不过也有scrapy内置的特殊key,也非常有用,它们如下:

可以设置http或https代理

request.meta['proxy'] = 'https://' + 'ip:port'
yield scrapy.Request(url= 'https://httpbin.org/get/zarten', meta= {'handle_httpstatus_list' : [404]})

在parse函数中可以看到处理404错误:   

def parse(self, response):      print('返回信息为:',response.text)
def start_requests(self):             urls = ['http://quotes.toscrape.com/page/1',                     'http://quotes.toscrape.com/page/3',                     'http://quotes.toscrape.com/page/5',                     ]             for i ,url in enumerate(urls):                 yield scrapy.Request(urlurl= url, meta= {'cookiejar' : i})         def parse(self, response):             next_page_url = response.css("li.next > a::attr(href)").extract_first()             if next_page_url is not None:                 yield scrapy.Request(response.urljoin(next_page_url), meta= {'cookiejar' : response.meta['cookiejar']}, callback= self.parse_next)         def parse_next(self, response):             print('cookiejar:', response.meta['cookiejar'])
def start_requests(self):             headers = {                 'user-agent' : 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36'             }             yield scrapy.Request(url= 'https://www.amazon.com', headersheaders= headers)         def parse(self, response):             print('响应时间为:', response.meta['download_latency'])

FormRequest

FormRequest 类为Request的子类,用于POST请求

这个类新增了一个参数 formdata,其他参数与Request一样,详细可参考上面的讲述

一般用法为: 

yield scrapy.FormRequest(url="http://www.example.com/post/action",                         formdata={'name': 'Zarten', 'age': '27'},                         callback=self.after_post)

看完上述内容,你们掌握如何进行Request的分析的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

推荐阅读:
  1. tomcat进行http request解析报错
  2. 爬虫基本原理及Request和Response分析

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

request

上一篇:Python matplotlib怎么绘制各种流线图

下一篇:Sqoop如何使用

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》