简体   繁体   English

如何使用scrapy获取数据?

[英]How to fetch data using scrapy?

I am working on a Django project and I want to provide some news feeds to the home page. 我正在研究Django项目,并且想向主页提供一些新闻提要。 I recently got interact with scrapy, when I run given code with "scarpy shell", this code is able to fetch the data successfully. 最近,我与scrapy进行了交互,当我使用“ scarpy shell”运行给定的代码时,该代码能够成功获取数据。 But when I put this code into a script to automate the newsfeed to the template this code wont work and raise "error fetch is not define" 但是,当我将这段代码放入脚本中以将新闻提要自动执行到模板中时,该代码将无法工作并引发“未定义错误提取”

import scrapy
from scrapy import *

fetch("https://www.google.co.in/search?q=cholera&safe=strict&source=lnms&tbm=nws&sa")

news_links = response.css(".r").extract()[0].encode('utf-8')
news_texts = response.css(".st").extract()[0].encode('utf-8')
news_images = response.css(".th").extract()[0].encode('utf-8')

I have tried this: 我已经试过了:

import scrapy


class QuotesSpider(scrapy.Spider):
    name = "quotes"

    def start_requests(self):
        urls = [
            'https://www.google.co.in/search?q=cholera&safe=strict&source=lnms&tbm=nws&sa']

        for url in urls:
            yield scrapy.Request(url=url, callback=self.parse)

    def parse(self, response):
        page = response.css(".r").extract()
        filename = 'quotes-%s.html' % page
        with open(filename, 'wb') as f:
            f.write(response.body)
        self.log('Saved file %s' % filename)

with the command: 使用命令:

scrapy crawl quotes

Which is also not working, How can I transform this code into a script. 这也行不通,如何将这段代码转换为脚本。

Error log: 错误日志:

scrapy crawl news
2017-12-22 14:24:33 [scrapy.utils.log] INFO: Scrapy 1.4.0 started (bot: newsfee)
2017-12-22 14:24:33 [scrapy.utils.log] INFO: Overridden settings: {'NEWSPIDER_M}
2017-12-22 14:24:33 [scrapy.middleware] INFO: Enabled extensions:
['scrapy.extensions.memusage.MemoryUsage',
 'scrapy.extensions.logstats.LogStats',
 'scrapy.extensions.telnet.TelnetConsole',
 'scrapy.extensions.corestats.CoreStats']
2017-12-22 14:24:33 [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.httpproxy.HttpProxyMiddleware',
 'scrapy.downloadermiddlewares.stats.DownloaderStats']
2017-12-22 14:24:33 [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-12-22 14:24:33 [scrapy.middleware] INFO: Enabled item pipelines:
[]
2017-12-22 14:24:33 [scrapy.core.engine] INFO: Spider opened
2017-12-22 14:24:33 [scrapy.extensions.logstats] INFO: Crawled 0 pages (at 0 pa)
2017-12-22 14:24:33 [scrapy.extensions.telnet] DEBUG: Telnet console listening 4
2017-12-22 14:24:33 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://www.)
2017-12-22 14:24:33 [scrapy.downloadermiddlewares.robotstxt] DEBUG: Forbidden b>
2017-12-22 14:24:33 [scrapy.core.engine] INFO: Closing spider (finished)
2017-12-22 14:24:33 [scrapy.statscollectors] INFO: Dumping Scrapy stats:
{'downloader/exception_count': 1,
 'downloader/exception_type_count/scrapy.exceptions.IgnoreRequest': 1,
 'downloader/request_bytes': 224,
 'downloader/request_count': 1,
 'downloader/request_method_count/GET': 1,
 'downloader/response_bytes': 2348,
 'downloader/response_count': 1,
 'downloader/response_status_count/200': 1,
 'finish_reason': 'finished',
 'finish_time': datetime.datetime(2017, 12, 22, 8, 54, 33, 954793),
 'log_count/DEBUG': 3,
 'log_count/INFO': 7,
 'memusage/max': 65478656,
 'memusage/startup': 65478656,
 'response_received_count': 1,
 'scheduler/dequeued': 1,
 'scheduler/dequeued/memory': 1,
 'scheduler/enqueued': 1,
 'scheduler/enqueued/memory': 1,
 'start_time': datetime.datetime(2017, 12, 22, 8, 54, 33, 295945)}
2017-12-22 14:24:33 [scrapy.core.engine] INFO: Spider closed (finished)
2017-12-22 14:24:33 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://www.)
2017-12-22 14:24:33 [scrapy.downloadermiddlewares.robotstxt] DEBUG: Forbidden b>

from this two lines log,it should have forbidden by google. 从这两行日志,它应该被谷歌禁止。

I think you need to firstly add up some more flexible and effective methods to avoid getting banned . 我认为您首先需要添加一些更灵活有效的方法来避免被禁止

Besides that,you can first use scrapy shell <url> to debug or test your scrapying url directly . 除此之外,您可以先使用scrapy shell <url>直接调试或测试您的scrapurl

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM