试图从Github页面中删除数据

时间:2017-01-18 09:43:29

标签: python scrapy

有人可以告诉我这有什么问题吗?我正在尝试使用命令" scrapy crawl gitrendscrap -o test.json"来刮取github页面并存储在JSON文件中。它创建了json文件,但它是空的。我试图在scrapy shell中运行单独的response.css文件。它完美地在那里工作。但由于某些原因,它不适用于蜘蛛。有人可以告诉我有什么问题吗?谢谢。

import scrapy


class gitrendscrap(scrapy.Spider):
    name = "gitrendscrap"
    start_urls = [
        'https://github.com/google',
    ]

    def parse(self, response):
        toplanguages = response.css("div.simple-box a span.mr-2::text").extract()
        titles=response.css("div.org-repos li.col-12 div.d-inline-block a::text").extract()
        description=response.css("div p.col-9::text").extract()
        language = response.css("div.f6 span.mr-3::text").extract()  

        for ele in toplanguages:
            ele = ele.encode('ascii','ignore').strip()

        for ele in titles:
            ele= ele.encode('ascii','ignore').strip() 

        for ele in description:
            ele = ele.encode('ascii','ignore').strip()

        for ele in language:
            ele = ele.encode('ascii','ignore').strip()

        yield{
                'title': titles,
                'description':description,
                'language': language,
                'toplanguage': toplanguages,
        }

        next_page = response.css('div.paginate-container a::attr(href)').extract_first()
        if next_page is not None:
            next_page = response.urljoin(next_page)
            yield scrapy.Request(next_page, callback=self.parse)

这是我蜘蛛的终端输出。

2017-01-18 02:29:45 [scrapy.utils.log] INFO: Scrapy 1.3.0 started (bot: gitrendscrap)
2017-01-18 02:29:45 [scrapy.utils.log] INFO: Overridden settings: {'NEWSPIDER_MODULE': 'gitrendscrap.spiders', 'FEED_URI': 'asd.json', 'SPIDER_MODULES': ['gitrendscrap.spiders'], 'BOT_NAME': 'gitrendscrap', 'ROBOTSTXT_OBEY': True, 'FEED_FORMAT': 'json'}
2017-01-18 02:29:45 [scrapy.middleware] INFO: Enabled extensions:
['scrapy.extensions.feedexport.FeedExporter',
 'scrapy.extensions.logstats.LogStats',
 'scrapy.extensions.telnet.TelnetConsole',
 'scrapy.extensions.corestats.CoreStats']
2017-01-18 02:29:45 [scrapy.middleware] INFO: Enabled downloader middlewares:
['scrapy.downloadermiddlewares.robotstxt.RobotsTxtMiddleware',
 'scrapy.downloadermiddlewares.httpauth.HttpAuthMiddleware',
 'scrapy.downloadermiddlewares.downloadtimeout.DownloadTimeoutMiddleware',
 'scrapy.downloadermiddlewares.defaultheaders.DefaultHeadersMiddleware',
 'scrapy.downloadermiddlewares.useragent.UserAgentMiddleware',
 'scrapy.downloadermiddlewares.retry.RetryMiddleware',
 'scrapy.downloadermiddlewares.redirect.MetaRefreshMiddleware',
 'scrapy.downloadermiddlewares.httpcompression.HttpCompressionMiddleware',
 'scrapy.downloadermiddlewares.redirect.RedirectMiddleware',
 'scrapy.downloadermiddlewares.cookies.CookiesMiddleware',
 'scrapy.downloadermiddlewares.stats.DownloaderStats']
2017-01-18 02:29:45 [scrapy.middleware] INFO: Enabled spider middlewares:
['scrapy.spidermiddlewares.httperror.HttpErrorMiddleware',
 'scrapy.spidermiddlewares.offsite.OffsiteMiddleware',
 'scrapy.spidermiddlewares.referer.RefererMiddleware',
 'scrapy.spidermiddlewares.urllength.UrlLengthMiddleware',
 'scrapy.spidermiddlewares.depth.DepthMiddleware']
2017-01-18 02:29:45 [scrapy.middleware] INFO: Enabled item pipelines:
[]
2017-01-18 02:29:45 [scrapy.core.engine] INFO: Spider opened
2017-01-18 02:29:45 [scrapy.extensions.logstats] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2017-01-18 02:29:45 [scrapy.extensions.telnet] DEBUG: Telnet console listening on 127.0.0.1:6023
2017-01-18 02:29:46 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://github.com/robots.txt> (referer: None)
2017-01-18 02:29:46 [scrapy.downloadermiddlewares.robotstxt] DEBUG: Forbidden by robots.txt: <GET https://github.com/google>
2017-01-18 02:29:46 [scrapy.core.engine] INFO: Closing spider (finished)
2017-01-18 02:29:46 [scrapy.statscollectors] INFO: Dumping Scrapy stats:
{'downloader/exception_count': 1,
 'downloader/exception_type_count/scrapy.exceptions.IgnoreRequest': 1,
 'downloader/request_bytes': 218,
 'downloader/request_count': 1,
 'downloader/request_method_count/GET': 1,
 'downloader/response_bytes': 1381,
 'downloader/response_count': 1,
 'downloader/response_status_count/200': 1,
 'finish_reason': 'finished',
 'finish_time': datetime.datetime(2017, 1, 18, 9, 29, 46, 320372),
 'log_count/DEBUG': 3,
 'log_count/INFO': 7,
 'response_received_count': 1,
 'scheduler/dequeued': 1,
 'scheduler/dequeued/memory': 1,
 'scheduler/enqueued': 1,
 'scheduler/enqueued/memory': 1,
 'start_time': datetime.datetime(2017, 1, 18, 9, 29, 45, 755711)}
2017-01-18 02:29:46 [scrapy.core.engine] INFO: Spider closed (finished)

1 个答案:

答案 0 :(得分:2)

仔细查看调试信息。

这一行:

2017-01-18 02:29:46 [scrapy.downloadermiddlewares.robotstxt] DEBUG: Forbidden by robots.txt: <GET https://github.com/google>

告诉您https://github.com/robots.txt禁止您抓取https://github.com/google。你不允许这样做,因为GitHub明确禁止它。 Scrapy爬虫,是好机器人,服从robots.txt。有一种方法可以忽略scrapy中的robots.txt,但我不会告诉你如何去做,因为这在道德上是有问题的。

编辑:我应该指出robots.txt允许以下内容:

Allow: /*/*/tree/master
Allow: /*/*/blob/master

因此,您可以尝试抓取,例如https://github.com/google/flexbox-layout/tree/master/,用flexbox-layout替换您想要的回购。

或者,写信给support@github.com或使用API​​作为robots.txt建议。