From d79a436960bf06ecfd9c99870044a63e7c2c41d7 Mon Sep 17 00:00:00 2001 From: Marvin Zhang Date: Sun, 17 Feb 2019 11:14:22 +0800 Subject: [PATCH] update SpiderDetail --- .idea/httpRequests/http-requests-log.http | 81 ++++++++++++++ app.py | 1 + constants/file.py | 3 + routes/files.py | 35 ++++++ spiders/taobao/scrapy.cfg | 11 ++ spiders/taobao/taobao/__init__.py | 0 spiders/taobao/taobao/items.py | 14 +++ spiders/taobao/taobao/middlewares.py | 103 ++++++++++++++++++ spiders/taobao/taobao/pipelines.py | 11 ++ spiders/taobao/taobao/settings.py | 90 +++++++++++++++ spiders/taobao/taobao/spiders/__init__.py | 4 + .../taobao/taobao/spiders/taobao_spider.py | 11 ++ test/test.http | 7 ++ 13 files changed, 371 insertions(+) create mode 100644 constants/file.py create mode 100644 routes/files.py create mode 100644 spiders/taobao/scrapy.cfg create mode 100644 spiders/taobao/taobao/__init__.py create mode 100644 spiders/taobao/taobao/items.py create mode 100644 spiders/taobao/taobao/middlewares.py create mode 100644 spiders/taobao/taobao/pipelines.py create mode 100644 spiders/taobao/taobao/settings.py create mode 100644 spiders/taobao/taobao/spiders/__init__.py create mode 100644 spiders/taobao/taobao/spiders/taobao_spider.py diff --git a/.idea/httpRequests/http-requests-log.http b/.idea/httpRequests/http-requests-log.http index dbd2e685..845a3e05 100644 --- a/.idea/httpRequests/http-requests-log.http +++ b/.idea/httpRequests/http-requests-log.http @@ -1,3 +1,84 @@ +GET http://localhost:5000/api/files?path=/Users/yeqing/projects/crawlab +Content-Type: application/json + +{} + +<> 2019-02-17T105049.200.json + +### + +GET http://localhost:5000/api/files?path=/Users/yeqing/projects/crawlab +Content-Type: application/json + +{} + +<> 2019-02-17T105013.200.json + +### + +GET http://localhost:5000/api/files?path=/Users/yeqing/projects/crawlab-frontend/src +Content-Type: application/json + +{} + +<> 2019-02-17T104952.200.json + +### + +GET http://localhost:5000/api/files?path=/Users/yeqing/projects/crawlab-frontend +Content-Type: application/json + +{} + +<> 2019-02-17T104905.200.json + +### + +GET http://localhost:5000/api/files?path=/Users/yeqing/projects/crawlab-frontend +Content-Type: application/json + +{} + +<> 2019-02-17T104843.200.json + +### + +GET http://localhost:5000/api/files?path=/Users/yeqing/projects/crawlab-frontend +Content-Type: application/json + +{} + +<> 2019-02-17T104612.200.json + +### + +GET http://localhost:5000/api/files?path=/Users/yeqing/projects/crawlab-frontend +Content-Type: application/json + +{} + +<> 2019-02-17T104523.400.json + +### + +GET http://localhost:5000/api/file?path=/Users/yeqing/projects/crawlab-frontend +Content-Type: application/json + +{} + +<> 2019-02-17T104518.404.html + +### + +GET http://localhost:5000/api/file?path=/Users/yeqing/projects/crawlab-frontend +Content-Type: application/json + +{} + +<> 2019-02-17T104351.404.html + +### + PUT http://localhost:5000/api/spiders Content-Type: application/json diff --git a/app.py b/app.py index cfd66941..92e97ff1 100644 --- a/app.py +++ b/app.py @@ -18,6 +18,7 @@ import routes.nodes import routes.spiders import routes.deploys import routes.tasks +import routes.files import routes.test # start flask app diff --git a/constants/file.py b/constants/file.py new file mode 100644 index 00000000..7418936e --- /dev/null +++ b/constants/file.py @@ -0,0 +1,3 @@ +class FileType: + FILE = 1 + FOLDER = 2 diff --git a/routes/files.py b/routes/files.py new file mode 100644 index 00000000..651bc971 --- /dev/null +++ b/routes/files.py @@ -0,0 +1,35 @@ +import os + +from flask_restful import reqparse, Resource + +from app import api +from utils import jsonify + + +class FileApi(Resource): + parser = reqparse.RequestParser() + arguments = [] + + def __init__(self): + super(FileApi).__init__() + self.parser.add_argument('path', type=str) + + def get(self): + args = self.parser.parse_args() + path = args.get('path') + folders = [] + files = [] + for _path in os.listdir(path): + if os.path.isfile(os.path.join(path, _path)): + files.append(_path) + elif os.path.isdir(os.path.join(_path)): + folders.append(_path) + return jsonify({ + 'status': 'ok', + 'files': sorted(files), + 'folders': sorted(folders), + }) + + +api.add_resource(FileApi, + '/api/files') diff --git a/spiders/taobao/scrapy.cfg b/spiders/taobao/scrapy.cfg new file mode 100644 index 00000000..c0e3980d --- /dev/null +++ b/spiders/taobao/scrapy.cfg @@ -0,0 +1,11 @@ +# Automatically created by: scrapy startproject +# +# For more information about the [deploy] section see: +# https://scrapyd.readthedocs.io/en/latest/deploy.html + +[settings] +default = taobao.settings + +[deploy] +#url = http://localhost:6800/ +project = taobao diff --git a/spiders/taobao/taobao/__init__.py b/spiders/taobao/taobao/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/spiders/taobao/taobao/items.py b/spiders/taobao/taobao/items.py new file mode 100644 index 00000000..322bd0fa --- /dev/null +++ b/spiders/taobao/taobao/items.py @@ -0,0 +1,14 @@ +# -*- coding: utf-8 -*- + +# Define here the models for your scraped items +# +# See documentation in: +# https://doc.scrapy.org/en/latest/topics/items.html + +import scrapy + + +class TaobaoItem(scrapy.Item): + # define the fields for your item here like: + # name = scrapy.Field() + pass diff --git a/spiders/taobao/taobao/middlewares.py b/spiders/taobao/taobao/middlewares.py new file mode 100644 index 00000000..afc752ba --- /dev/null +++ b/spiders/taobao/taobao/middlewares.py @@ -0,0 +1,103 @@ +# -*- coding: utf-8 -*- + +# Define here the models for your spider middleware +# +# See documentation in: +# https://doc.scrapy.org/en/latest/topics/spider-middleware.html + +from scrapy import signals + + +class TaobaoSpiderMiddleware(object): + # Not all methods need to be defined. If a method is not defined, + # scrapy acts as if the spider middleware does not modify the + # passed objects. + + @classmethod + def from_crawler(cls, crawler): + # This method is used by Scrapy to create your spiders. + s = cls() + crawler.signals.connect(s.spider_opened, signal=signals.spider_opened) + return s + + def process_spider_input(self, response, spider): + # Called for each response that goes through the spider + # middleware and into the spider. + + # Should return None or raise an exception. + return None + + def process_spider_output(self, response, result, spider): + # Called with the results returned from the Spider, after + # it has processed the response. + + # Must return an iterable of Request, dict or Item objects. + for i in result: + yield i + + def process_spider_exception(self, response, exception, spider): + # Called when a spider or process_spider_input() method + # (from other spider middleware) raises an exception. + + # Should return either None or an iterable of Response, dict + # or Item objects. + pass + + def process_start_requests(self, start_requests, spider): + # Called with the start requests of the spider, and works + # similarly to the process_spider_output() method, except + # that it doesn’t have a response associated. + + # Must return only requests (not items). + for r in start_requests: + yield r + + def spider_opened(self, spider): + spider.logger.info('Spider opened: %s' % spider.name) + + +class TaobaoDownloaderMiddleware(object): + # Not all methods need to be defined. If a method is not defined, + # scrapy acts as if the downloader middleware does not modify the + # passed objects. + + @classmethod + def from_crawler(cls, crawler): + # This method is used by Scrapy to create your spiders. + s = cls() + crawler.signals.connect(s.spider_opened, signal=signals.spider_opened) + return s + + def process_request(self, request, spider): + # Called for each request that goes through the downloader + # middleware. + + # Must either: + # - return None: continue processing this request + # - or return a Response object + # - or return a Request object + # - or raise IgnoreRequest: process_exception() methods of + # installed downloader middleware will be called + return None + + def process_response(self, request, response, spider): + # Called with the response returned from the downloader. + + # Must either; + # - return a Response object + # - return a Request object + # - or raise IgnoreRequest + return response + + def process_exception(self, request, exception, spider): + # Called when a download handler or a process_request() + # (from other downloader middleware) raises an exception. + + # Must either: + # - return None: continue processing this exception + # - return a Response object: stops process_exception() chain + # - return a Request object: stops process_exception() chain + pass + + def spider_opened(self, spider): + spider.logger.info('Spider opened: %s' % spider.name) diff --git a/spiders/taobao/taobao/pipelines.py b/spiders/taobao/taobao/pipelines.py new file mode 100644 index 00000000..21db0b84 --- /dev/null +++ b/spiders/taobao/taobao/pipelines.py @@ -0,0 +1,11 @@ +# -*- coding: utf-8 -*- + +# Define your item pipelines here +# +# Don't forget to add your pipeline to the ITEM_PIPELINES setting +# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html + + +class TaobaoPipeline(object): + def process_item(self, item, spider): + return item diff --git a/spiders/taobao/taobao/settings.py b/spiders/taobao/taobao/settings.py new file mode 100644 index 00000000..b68be261 --- /dev/null +++ b/spiders/taobao/taobao/settings.py @@ -0,0 +1,90 @@ +# -*- coding: utf-8 -*- + +# Scrapy settings for taobao project +# +# For simplicity, this file contains only settings considered important or +# commonly used. You can find more settings consulting the documentation: +# +# https://doc.scrapy.org/en/latest/topics/settings.html +# https://doc.scrapy.org/en/latest/topics/downloader-middleware.html +# https://doc.scrapy.org/en/latest/topics/spider-middleware.html + +BOT_NAME = 'taobao' + +SPIDER_MODULES = ['taobao.spiders'] +NEWSPIDER_MODULE = 'taobao.spiders' + + +# Crawl responsibly by identifying yourself (and your website) on the user-agent +#USER_AGENT = 'taobao (+http://www.yourdomain.com)' + +# Obey robots.txt rules +ROBOTSTXT_OBEY = True + +# Configure maximum concurrent requests performed by Scrapy (default: 16) +#CONCURRENT_REQUESTS = 32 + +# Configure a delay for requests for the same website (default: 0) +# See https://doc.scrapy.org/en/latest/topics/settings.html#download-delay +# See also autothrottle settings and docs +#DOWNLOAD_DELAY = 3 +# The download delay setting will honor only one of: +#CONCURRENT_REQUESTS_PER_DOMAIN = 16 +#CONCURRENT_REQUESTS_PER_IP = 16 + +# Disable cookies (enabled by default) +#COOKIES_ENABLED = False + +# Disable Telnet Console (enabled by default) +#TELNETCONSOLE_ENABLED = False + +# Override the default request headers: +#DEFAULT_REQUEST_HEADERS = { +# 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', +# 'Accept-Language': 'en', +#} + +# Enable or disable spider middlewares +# See https://doc.scrapy.org/en/latest/topics/spider-middleware.html +#SPIDER_MIDDLEWARES = { +# 'taobao.middlewares.TaobaoSpiderMiddleware': 543, +#} + +# Enable or disable downloader middlewares +# See https://doc.scrapy.org/en/latest/topics/downloader-middleware.html +#DOWNLOADER_MIDDLEWARES = { +# 'taobao.middlewares.TaobaoDownloaderMiddleware': 543, +#} + +# Enable or disable extensions +# See https://doc.scrapy.org/en/latest/topics/extensions.html +#EXTENSIONS = { +# 'scrapy.extensions.telnet.TelnetConsole': None, +#} + +# Configure item pipelines +# See https://doc.scrapy.org/en/latest/topics/item-pipeline.html +#ITEM_PIPELINES = { +# 'taobao.pipelines.TaobaoPipeline': 300, +#} + +# Enable and configure the AutoThrottle extension (disabled by default) +# See https://doc.scrapy.org/en/latest/topics/autothrottle.html +#AUTOTHROTTLE_ENABLED = True +# The initial download delay +#AUTOTHROTTLE_START_DELAY = 5 +# The maximum download delay to be set in case of high latencies +#AUTOTHROTTLE_MAX_DELAY = 60 +# The average number of requests Scrapy should be sending in parallel to +# each remote server +#AUTOTHROTTLE_TARGET_CONCURRENCY = 1.0 +# Enable showing throttling stats for every response received: +#AUTOTHROTTLE_DEBUG = False + +# Enable and configure HTTP caching (disabled by default) +# See https://doc.scrapy.org/en/latest/topics/downloader-middleware.html#httpcache-middleware-settings +#HTTPCACHE_ENABLED = True +#HTTPCACHE_EXPIRATION_SECS = 0 +#HTTPCACHE_DIR = 'httpcache' +#HTTPCACHE_IGNORE_HTTP_CODES = [] +#HTTPCACHE_STORAGE = 'scrapy.extensions.httpcache.FilesystemCacheStorage' diff --git a/spiders/taobao/taobao/spiders/__init__.py b/spiders/taobao/taobao/spiders/__init__.py new file mode 100644 index 00000000..ebd689ac --- /dev/null +++ b/spiders/taobao/taobao/spiders/__init__.py @@ -0,0 +1,4 @@ +# This package will contain the spiders of your Scrapy project +# +# Please refer to the documentation for information on how to create and manage +# your spiders. diff --git a/spiders/taobao/taobao/spiders/taobao_spider.py b/spiders/taobao/taobao/spiders/taobao_spider.py new file mode 100644 index 00000000..c25fbca2 --- /dev/null +++ b/spiders/taobao/taobao/spiders/taobao_spider.py @@ -0,0 +1,11 @@ +# -*- coding: utf-8 -*- +import scrapy + + +class TaobaoSpiderSpider(scrapy.Spider): + name = 'taobao-spider' + allowed_domains = ['taobao.com'] + start_urls = ['http://taobao.com/'] + + def parse(self, response): + pass diff --git a/test/test.http b/test/test.http index d15577fc..3cbd210c 100644 --- a/test/test.http +++ b/test/test.http @@ -43,4 +43,11 @@ Content-Type: application/json {} +### +### Send GET request with json body by path +GET http://localhost:5000/api/files?path=/Users/yeqing/projects/crawlab +Content-Type: application/json + +{} + ### \ No newline at end of file