国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

腳本之家,腳本語言編程技術及教程分享平臺!
分類導航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務器之家 - 腳本之家 - Python - Scrapy-Redis結合POST請求獲取數據的方法示例

Scrapy-Redis結合POST請求獲取數據的方法示例

2021-06-24 00:10Hi!Roy! Python

這篇文章主要給大家介紹了關于Scrapy-Redis結合POST請求獲取數據的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用Scrapy-Redis具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧

前言

通常我們在一個站站點進行采集的時候,如果是小站的話 我們使用scrapy本身就可以滿足。

但是如果在面對一些比較大型的站點的時候,單個scrapy就顯得力不從心了。

要是我們能夠多個Scrapy一起采集該多好啊 人多力量大。

很遺憾Scrapy官方并不支持多個同時采集一個站點,雖然官方給出一個方法:

**將一個站點的分割成幾部分 交給不同的scrapy去采集**

似乎是個解決辦法,但是很麻煩誒!畢竟分割很麻煩的哇

下面就改輪到我們的額主角Scrapy-Redis登場了!

能看到這篇文章的小伙伴肯定已經知道什么是Scrapy以及Scrapy-Redis了,基礎概念這里就不再介紹。默認情況下Scrapy-Redis是發送GET請求獲取數據的,對于某些使用POST請求的情況需要重寫make_request_from_data函數即可,但奇怪的是居然沒在網上搜到簡潔明了的答案,或許是太簡單了?。

這里我以httpbin.org這個網站為例,首先在settings.py中添加所需配置,這里需要根據實際情況進行修改:

?
1
2
3
4
5
SCHEDULER = "scrapy_redis.scheduler.Scheduler" #啟用Redis調度存儲請求隊列
SCHEDULER_PERSIST = True #不清除Redis隊列、這樣可以暫停/恢復 爬取
DUPEFILTER_CLASS = "scrapy_redis.dupefilter.RFPDupeFilter" #確保所有的爬蟲通過Redis去重
SCHEDULER_QUEUE_CLASS = 'scrapy_redis.queue.SpiderPriorityQueue'
REDIS_URL = "redis://127.0.0.1:6379"

爬蟲代碼如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# -*- coding: utf-8 -*-
import scrapy
from scrapy_redis.spiders import RedisSpider
 
 
class HpbSpider(RedisSpider):
 name = 'hpb'
 redis_key = 'test_post_data'
 
 def make_request_from_data(self, data):
  """Returns a Request instance from data coming from Redis.
  By default, ``data`` is an encoded URL. You can override this method to
  provide your own message decoding.
  Parameters
  ----------
  data : bytes
   Message from redis.
  """
  return scrapy.FormRequest("https://www.httpbin.org/post",
         formdata={"data":data},callback=self.parse)
 
 def parse(self, response):
  print(response.body)

這里為了簡單直接進行輸出,真實使用時可以結合pipeline寫數據庫等。

然后啟動爬蟲程序scrapy crawl hpb,由于我們還沒向test_post_data中寫數據,所以啟動后程序進入等待狀態。然后模擬向隊列寫數據:

?
1
2
3
4
import redis
rd = redis.Redis('127.0.0.1',port=6379,db=0)
for _ in range(1000):
 rd.lpush('test_post_data',_)

此時可以看到爬蟲已經開始獲取程序了:

2019-05-06 16:30:21 [hpb] DEBUG: Read 8 requests from 'test_post_data'
2019-05-06 16:30:21 [scrapy.core.engine] DEBUG: Crawled (200) <POST https://www.httpbin.org/post> (referer: None)
2019-05-06 16:30:21 [scrapy.core.engine] DEBUG: Crawled (200) <POST https://www.httpbin.org/post> (referer: None)
2019-05-06 16:30:21 [scrapy.core.engine] DEBUG: Crawled (200) <POST https://www.httpbin.org/post> (referer: None)
2019-05-06 16:30:21 [scrapy.core.engine] DEBUG: Crawled (200) <POST https://www.httpbin.org/post> (referer: None)
2019-05-06 16:30:21 [scrapy.core.engine] DEBUG: Crawled (200) <POST https://www.httpbin.org/post> (referer: None)
2019-05-06 16:30:21 [scrapy.core.engine] DEBUG: Crawled (200) <POST https://www.httpbin.org/post> (referer: None)
2019-05-06 16:30:21 [scrapy.core.engine] DEBUG: Crawled (200) <POST https://www.httpbin.org/post> (referer: None)
2019-05-06 16:30:21 [scrapy.core.engine] DEBUG: Crawled (200) <POST https://www.httpbin.org/post> (referer: None)
b'{\n  "args": {}, \n  "data": "", \n  "files": {}, \n  "form": {\n    "data": "0"\n  }, \n  "headers": {\n    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", \n    "Accept-Encoding": "gzip,deflate", \n    "Accept-Language": "en", \n    "Content-Length": "6", \n    "Content-Type": "application/x-www-form-urlencoded", \n    "Host": "www.httpbin.org", \n    "User-Agent": "Scrapy/1.5.1 (+https://scrapy.org)"\n  }, \n  "json": null, \n  "origin": "1.2.3.48, 1.2.3.48", \n  "url": "https://www.httpbin.org/post"\n}\n'
b'{\n  "args": {}, \n  "data": "", \n  "files": {}, \n  "form": {\n    "data": "1"\n  }, \n  "headers": {\n    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", \n    "Accept-Encoding": "gzip,deflate", \n    "Accept-Language": "en", \n    "Content-Length": "6", \n    "Content-Type": "application/x-www-form-urlencoded", \n    "Host": "www.httpbin.org", \n    "User-Agent": "Scrapy/1.5.1 (+https://scrapy.org)"\n  }, \n  "json": null, \n  "origin": "1.2.3.48, 1.2.3.48", \n  "url": "https://www.httpbin.org/post"\n}\n'
b'{\n  "args": {}, \n  "data": "", \n  "files": {}, \n  "form": {\n    "data": "3"\n  }, \n  "headers": {\n    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", \n    "Accept-Encoding": "gzip,deflate", \n    "Accept-Language": "en", \n    "Content-Length": "6", \n    "Content-Type": "application/x-www-form-urlencoded", \n    "Host": "www.httpbin.org", \n    "User-Agent": "Scrapy/1.5.1 (+https://scrapy.org)"\n  }, \n  "json": null, \n  "origin": "1.2.3.48, 1.2.3.48", \n  "url": "https://www.httpbin.org/post"\n}\n'
b'{\n  "args": {}, \n  "data": "", \n  "files": {}, \n  "form": {\n    "data": "2"\n  }, \n  "headers": {\n    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", \n    "Accept-Encoding": "gzip,deflate", \n    "Accept-Language": "en", \n    "Content-Length": "6", \n    "Content-Type": "application/x-www-form-urlencoded", \n    "Host": "www.httpbin.org", \n    "User-Agent": "Scrapy/1.5.1 (+https://scrapy.org)"\n  }, \n  "json": null, \n  "origin": "1.2.3.48, 1.2.3.48", \n  "url": "https://www.httpbin.org/post"\n}\n'
b'{\n  "args": {}, \n  "data": "", \n  "files": {}, \n  "form": {\n    "data": "4"\n  }, \n  "headers": {\n    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", \n    "Accept-Encoding": "gzip,deflate", \n    "Accept-Language": "en", \n    "Content-Length": "6", \n    "Content-Type": "application/x-www-form-urlencoded", \n    "Host": "www.httpbin.org", \n    "User-Agent": "Scrapy/1.5.1 (+https://scrapy.org)"\n  }, \n  "json": null, \n  "origin": "1.2.3.48, 1.2.3.48", \n  "url": "https://www.httpbin.org/post"\n}\n'
b'{\n  "args": {}, \n  "data": "", \n  "files": {}, \n  "form": {\n    "data": "5"\n  }, \n  "headers": {\n    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", \n    "Accept-Encoding": "gzip,deflate", \n    "Accept-Language": "en", \n    "Content-Length": "6", \n    "Content-Type": "application/x-www-form-urlencoded", \n    "Host": "www.httpbin.org", \n    "User-Agent": "Scrapy/1.5.1 (+https://scrapy.org)"\n  }, \n  "json": null, \n  "origin": "1.2.3.48, 1.2.3.48", \n  "url": "https://www.httpbin.org/post"\n}\n'
b'{\n  "args": {}, \n  "data": "", \n  "files": {}, \n  "form": {\n    "data": "6"\n  }, \n  "headers": {\n    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", \n    "Accept-Encoding": "gzip,deflate", \n    "Accept-Language": "en", \n    "Content-Length": "6", \n    "Content-Type": "application/x-www-form-urlencoded", \n    "Host": "www.httpbin.org", \n    "User-Agent": "Scrapy/1.5.1 (+https://scrapy.org)"\n  }, \n  "json": null, \n  "origin": "1.2.3.48, 1.2.3.48", \n  "url": "https://www.httpbin.org/post"\n}\n'
b'{\n  "args": {}, \n  "data": "", \n  "files": {}, \n  "form": {\n    "data": "7"\n  }, \n  "headers": {\n    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", \n    "Accept-Encoding": "gzip,deflate", \n    "Accept-Language": "en", \n    "Content-Length": "6", \n    "Content-Type": "application/x-www-form-urlencoded", \n    "Host": "www.httpbin.org", \n    "User-Agent": "Scrapy/1.5.1 (+https://scrapy.org)"\n  }, \n  "json": null, \n  "origin": "1.2.3.48, 1.2.3.48", \n  "url": "https://www.httpbin.org/post"\n}\n'
2019-05-06 16:31:09 [scrapy.extensions.logstats] INFO: Crawled 1001 pages (at 280 pages/min), scraped 0 items (at 0 items/min)
2019-05-06 16:32:09 [scrapy.extensions.logstats] INFO: Crawled 1001 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2019-05-06 16:33:09 [scrapy.extensions.logstats] INFO: Crawled 1001 pages (at 0 pages/min), scraped 0 items (at 0 items/min)

至于數據重復的問題,如果POST的數據重復,這個請求就不會發送出去。如果有特殊情況POST發送同樣的數據回得到不同返回值,添加dont_filter=True是沒用的,在RFPDupeFilter類中并沒考慮這個參數,需要重寫。

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對服務器之家的支持。

原文鏈接:http://www.hi-roy.com/2019/05/06/Scrapy-Redis結合POST請求獲取數據/

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 成人区精品一区二区毛片不卡 | 午夜电影 | 欧美.com | 亚洲狠狠爱一区二区三区 | 在线视频一区二区三区 | 超碰在线免费福利 | 精品久久久久久久久久久 | 欧美综合在线观看 | 毛片免费观看视频 | 午夜影剧院| 天天插天天操 | 岛国av免费| 国产一区二区三区在线看 | 国产成人一区二区在线观看 | 国产一区二区av | 欧美一区二 | 欧美国产精品一区二区三区 | 国产美女视频网站 | 精品福利片 | 亚州男人天堂 | 成人国产精品免费观看 | 精品久久久久久久久久久 | 成人午夜网 | 91精品入口蜜桃 | 免费在线a | 国产日韩欧美 | 激情毛片 | 国产脚交av在线一区二区 | 欧美精品国产精品 | 亚洲三区在线观看 | 人人爱人人爽 | 国产日韩欧美精品 | 看av的网址 | 在线观看一区二区三区四区 | 久久综合国产 | a免费网站| 亚洲国产免费av | 精品国产青草久久久久福利 | 免费观看一区二区三区毛片软件 | 国产精品成人一区二区三区夜夜夜 | 黄色三级视频 |