Python implements the method of running Scrapy in the thread

Source: Internet
Author: User
The examples in this paper describe how Python implements scrapy in a thread. Share to everyone for your reference. Specific as follows:

If you want to call Scrapy in a well-written program, you can use the following code to let Scrapy run in a thread.

"" "Code to run Scrapy crawler in a thread-works on scrapy 0.8" "Import threading, Queuefrom twisted.internet import REAC Torfrom scrapy.xlib.pydispatch Import dispatcherfrom scrapy.core.manager import Scrapymanagerfrom scrapy.core.engine Import scrapyenginefrom scrapy.core import Signalsclass crawlerthread (threading. Thread): def __init__ (self): threading. Thread.__init__ (self) self.running = False def run (self): self.running = True scrapymanager.configure (control_re Actor=false) Scrapymanager.start () Reactor.run (Installsignalhandlers=false) def crawl (self, *args): If not self. Running:raise RuntimeError ("Crawlerthread not Running") self._call_and_block_until_signal (signals.spider_closed, \ Scrapymanager.crawl, *args) def stop (self): Reactor.callfromthread (scrapyengine.stop) def _call_and_block_unti L_signal (self, signal, F, *a, **kw): Q = queue.queue () def unblock (): Q.put (None) dispatcher.connect (unblock , signal=signal) reactor.Callfromthread (f, *a, **kw) Q.get () # Usage example Below:import osos.environ.setdefault (' scrapy_settings_module ', ' MYP Roject.settings ') from Scrapy.xlib.pydispatch import dispatcherfrom scrapy.core import signalsfrom scrapy.conf Import Settingsfrom scrapy.crawler import crawlerthreadsettings.overrides[' log_enabled '] = False # avoid LOG noisedef item_ Passed (item): print "Just scraped item:", Itemdispatcher.connect (item_passed, signal=signals.item_passed) crawler = Crawlerthread () print "Starting crawler thread ..." Crawler.start () print "Crawling somedomain.com ..." crawler.crawl (' somedomain.com) # blocking Callprint "crawling anotherdomain.com ..." crawler.crawl (' anotherdomain.com ') # Blocking Callprint "Stopping crawler thread ..." Crawler.stop ()

Hopefully this article will help you with Python programming.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.