Scrapy 抓取数据但没有输出到文件 [英] Scrapy scrapes data but no output to file

查看:226
本文介绍了Scrapy 抓取数据但没有输出到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尽管能够在scrapy shell 中成功执行大部分行,但我还是得到了空白的json 文件.当我运行命令 scrapy crawl course 时,我的课程机器人是:

I've been getting blank json files despite successfully being able to execute most of the lines in scrapy shell. When I run the command scrapy crawl courses with my courses bot being:

from scrapy.spiders import CrawlSpider
from scrapy.linkextractors import LinkExtractor
from tutorial.items import CoursesItem
from bs4 import BeautifulSoup
import scrapy

class CoursesSpider(CrawlSpider):
    name = 'courses'
    allowed_domains = ['guide.berkeley.edu']
    start_urls = ['http://guide.berkeley.edu/courses/ast', 
                   ]

def parse(self, response):
    soup = BeautifulSoup(response.body_as_unicode(), 'lxml')
    items = []
    for course_info, course_desc, course_req in zip(soup.find_all('p',class_='courseblocktitle'), \
                                                soup.find_all('p', class_='courseblockdesc'), \
                                                soup.find_all('div', class_='course-section')):     
        item = CoursesItem()
        item['title'] = course_info.text
        item['description'] = course_desc.text
        item['requirements'] = course_req.text
        yield items

并通过 settings.py

and by settings.py being

BOT_NAME = 'courses'

SPIDER_MODULES = ['tutorial.spiders']
NEWSPIDER_MODULE = 'tutorial.spiders'

USER_AGENT = 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:39.0) Gecko/20100101 Firefox/39.0.3'

# ITEM_PIPELINES = {
#   'tutorial.pipelines.JsonExportPipeline': 300
# }

FEED_URI = 'output.json'
FEED_FORMAT = 'json'

正如您在评论部分所看到的,我也尝试过制作管道.我的管道文件如下所示:

As you can see in the commented section, I've also tried making a pipeline. My pipeline file looks like this:

from scrapy import signals
from scrapy.exporters import JsonLinesItemExporter

class JsonExportPipeline(object):

    def __init__(self):
        self.files = {}

    @classmethod
    def from_crawler(cls, crawler):
        pipeline = cls()
        crawler.signals.connect(pipeline.spider_opened, signals.spider_opened)
        crawler.signals.connect(pipeline.spider_closed, signals.spider_closed)
        return pipeline

    def spider_opened(self, spider):
        file = open('%s_spider.json' % spider.name, 'w+b')
        self.files[spider] = file
        self.exporter = JsonLinesItemExporter(file)
        self.exporter.start_exporting()

    def spider_closed(self, spider):
        self.exporter.finish_exporting()
        file = self.files.pop(spider)
        file.close()

    def process_item(self, item, spider):
        self.exporter.export_item(item)
        return item

但我觉得这可能不是错误所在,尽管这是可能的,因为我主要遵循了我发现的几个教程.

But I feel that might not be where the error lies although it's possible since I largely followed a couple of tutorials I found.

我使用 BeautifulSoup 来简化我选择项目的方式.

I used BeautifulSoup to simplify the way I select the items.

最后但并非最不重要的是,终端在我运行后看起来像这样.

Last but not least, the terminal looks like this after I run it.

2015-08-07 23:58:44 [scrapy] INFO: Scrapy 1.0.1 started (bot: courses)
2015-08-07 23:58:44 [scrapy] INFO: Optional features available: ssl, http11
2015-08-07 23:58:44 [scrapy] INFO: Overridden settings: {'NEWSPIDER_MODULE': 'tu
torial.spiders', 'FEED_URI': 'output.json', 'SPIDER_MODULES': ['tutorial.spiders
'], 'BOT_NAME': 'courses', 'USER_AGENT': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv
:39.0) Gecko/20100101 Firefox/39.0.3', 'FEED_FORMAT': 'json'}
2015-08-07 23:58:44 [scrapy] INFO: Enabled extensions: CloseSpider, FeedExporter
, TelnetConsole, LogStats, CoreStats, SpiderState
2015-08-07 23:58:44 [scrapy] INFO: Enabled downloader middlewares: HttpAuthMiddl
eware, DownloadTimeoutMiddleware, UserAgentMiddleware, RetryMiddleware, DefaultH
eadersMiddleware, MetaRefreshMiddleware, HttpCompressionMiddleware, RedirectMidd
leware, CookiesMiddleware, ChunkedTransferMiddleware, DownloaderStats
2015-08-07 23:58:44 [scrapy] INFO: Enabled spider middlewares: HttpErrorMiddleware, OffsiteMiddleware, RefererMiddleware, UrlLengthMiddleware, DepthMiddleware
2015-08-07 23:58:44 [scrapy] INFO: Enabled item pipelines:
2015-08-07 23:58:44 [scrapy] INFO: Spider opened
2015-08-07 23:58:44 [scrapy] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 i
tems (at 0 items/min)
2015-08-07 23:58:44 [scrapy] DEBUG: Telnet console listening on 127.0.0.1:6024
2015-08-07 23:58:45 [scrapy] DEBUG: Redirecting (301) to <GET http://guide.berke
ley.edu/courses/ast/> from <GET http://guide.berkeley.edu/courses/ast>
2015-08-07 23:58:45 [scrapy] DEBUG: Crawled (200) <GET http://guide.berkeley.edu
/courses/ast/> (referer: None)
2015-08-07 23:58:45 [scrapy] INFO: Closing spider (finished)
2015-08-07 23:58:45 [scrapy] INFO: Dumping Scrapy stats:
{'downloader/request_bytes': 537,
 'downloader/request_count': 2,
 'downloader/request_method_count/GET': 2,
 'downloader/response_bytes': 22109,
 'downloader/response_count': 2,
 'downloader/response_status_count/200': 1,
 'downloader/response_status_count/301': 1,
 'finish_reason': 'finished',
 'finish_time': datetime.datetime(2015, 8, 8, 6, 58, 45, 600000),
 'log_count/DEBUG': 3,
 'log_count/INFO': 7,
 'response_received_count': 1,
 'scheduler/dequeued': 2,
 'scheduler/dequeued/memory': 2,
 'scheduler/enqueued': 2,
 'scheduler/enqueued/memory': 2,
 'start_time': datetime.datetime(2015, 8, 8, 6, 58, 44, 663000)}
2015-08-07 23:58:45 [scrapy] INFO: Spider closed (finished)

我已经彻底地运行了大部分选项.运行--parse 的单数选项告诉我我正在解析项目,但即便如此,我想知道除了解析错误修复(即输出到json)之外的地方.最终,我想将所有这些数据通过管道传输到数据库中.

I've ran most of my options thoroughly. Running the singular option of --parse tells me that I'm off in parsing the items, but even then, I'd like to know where to go beyond the parse bug fix (ie outputting to json). Ultimately, I want to pipe all this data into a database.

我知道要浏览的内容很多,但感谢您的帮助,谢谢!

I know it's a lot to look through, but any help is appreciated, thanks!

推荐答案

你写错了词.在解析函数中更改项目 -> 项目.

You write a wrong word. In parse function change items -> item.

def parse(self, response):
    soup = BeautifulSoup(response.body_as_unicode(), 'lxml')
    items = []
    for ...
        item = CoursesItem()
        item['title'] = course_info.text
        item['description'] = course_desc.text
        item['requirements'] = course_req.text
        yield items # -> item

`

这篇关于Scrapy 抓取数据但没有输出到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆