- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
使用 scrapy
framework
后,我想在我的 python
脚本中处理我的 pipeline.py
输出上下文。
管道输出为tracks.jl
,如下:
管道.py
class PitchforkTracks(object):
def __init__(self):
self.file = open('tracks.jl', 'wb')
在同一个script.py
目录
上生成一个json
文件:
def process_item(self, item, spider):
line = json.dumps(dict(item)) + "\n"
self.file.write(line)
return item
在这里,我运行scrapy
。
脚本.py
def output():
process = CrawlerProcess(get_project_settings())
response = process.crawl('pitchfork_tracks', domain='pitchfork.com')
# the script will block here until the crawling is finished
process.start()
#process pipelined file
tracks = []
time.sleep(5)
#here I pause so there is time for pipeline to build `tracks.jl` at directory
with open('tracks.jl', 'r+') as t:
for line in t:
tracks.append(json.loads(line))
print (tracks)
#process lines from here on
但是,当我打印lines
时,我得到一个空的list
[]
。
如何在python脚本
内打印pipeline.py
的输出?
编辑:
这是我运行scrapy scrappitchfork_tracks
时的日志
:
2016-10-03 09:28:39 [scrapy] INFO: Scrapy 1.1.2 started (bot: blogs)
2016-10-03 09:28:39 [scrapy] INFO: Overridden settings: {'NEWSPIDER_MODULE': 'blogs.spiders', 'SPIDER_MODULES': ['blogs.spiders'], 'ROBOTSTXT_OBEY': True, 'BOT_NAME': 'blogs'}
2016-10-03 09:28:39 [scrapy] INFO: Enabled extensions:
['scrapy.extensions.logstats.LogStats',
'scrapy.extensions.telnet.TelnetConsole',
'scrapy.extensions.corestats.CoreStats']
2016-10-03 09:28:39 [scrapy] INFO: Enabled downloader middlewares:
['scrapy.downloadermiddlewares.robotstxt.RobotsTxtMiddleware',
'scrapy.downloadermiddlewares.httpauth.HttpAuthMiddleware',
'scrapy.downloadermiddlewares.downloadtimeout.DownloadTimeoutMiddleware',
'scrapy.downloadermiddlewares.useragent.UserAgentMiddleware',
'scrapy.downloadermiddlewares.retry.RetryMiddleware',
'scrapy.downloadermiddlewares.defaultheaders.DefaultHeadersMiddleware',
'scrapy.downloadermiddlewares.redirect.MetaRefreshMiddleware',
'scrapy.downloadermiddlewares.httpcompression.HttpCompressionMiddleware',
'scrapy.downloadermiddlewares.redirect.RedirectMiddleware',
'scrapy.downloadermiddlewares.cookies.CookiesMiddleware',
'scrapy.downloadermiddlewares.chunked.ChunkedTransferMiddleware',
'scrapy.downloadermiddlewares.stats.DownloaderStats']
2016-10-03 09:28:39 [scrapy] INFO: Enabled spider middlewares:
['scrapy.spidermiddlewares.httperror.HttpErrorMiddleware',
'scrapy.spidermiddlewares.offsite.OffsiteMiddleware',
'scrapy.spidermiddlewares.referer.RefererMiddleware',
'scrapy.spidermiddlewares.urllength.UrlLengthMiddleware',
'scrapy.spidermiddlewares.depth.DepthMiddleware']
2016-10-03 09:28:39 [scrapy] INFO: Enabled item pipelines:
['blogs.pipelines.PitchforkTracks',
'blogs.pipelines.PitchforkAlbums',
'blogs.pipelines.PitchforkReissues']
2016-10-03 09:28:39 [scrapy] INFO: Spider opened
2016-10-03 09:28:39 [scrapy] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2016-10-03 09:28:39 [scrapy] DEBUG: Telnet console listening on 127.0.0.1:6023
2016-10-03 09:28:39 [scrapy] DEBUG: Crawled (200) <GET http://pitchfork.com/robots.txt> (referer: None)
2016-10-03 09:28:40 [scrapy] DEBUG: Crawled (200) <GET http://pitchfork.com/reviews/best/tracks/?page=1> (referer: None)
2016-10-03 09:28:40 [scrapy] DEBUG: Scraped from <200 http://pitchfork.com/reviews/best/tracks/?page=1>
{'artist': u'Moses Sumney', 'track': u'Lonely World'}
2016-10-03 09:28:40 [scrapy] DEBUG: Scraped from <200 http://pitchfork.com/reviews/best/tracks/?page=1>
{'artist': u'Cherry Glazerr', 'track': u"Told You I'd Be With the Guys"}
2016-10-03 09:28:40 [scrapy] DEBUG: Scraped from <200 http://pitchfork.com/reviews/best/tracks/?page=1>
{'artist': u'Danny Brown',
'track': u'Really Doe\u201d [ft. Kendrick Lamar, Ab-Soul, and Earl Sweatshirt]'}
2016-10-03 09:28:40 [scrapy] DEBUG: Scraped from <200 http://pitchfork.com/reviews/best/tracks/?page=1>
{'artist': u'NxWorries', 'track': u'Lyk Dis'}
2016-10-03 09:28:40 [scrapy] DEBUG: Scraped from <200 http://pitchfork.com/reviews/best/tracks/?page=1>
{'artist': u'Nick Cave & the Bad Seeds', 'track': u'I Need You'}
2016-10-03 09:28:40 [scrapy] DEBUG: Scraped from <200 http://pitchfork.com/reviews/best/tracks/?page=1>
{'artist': u'Merchandise', 'track': u'Lonesome Sound'}
2016-10-03 09:28:40 [scrapy] DEBUG: Scraped from <200 http://pitchfork.com/reviews/best/tracks/?page=1>
{'artist': u'Weyes Blood', 'track': u'Do You Need My Love'}
2016-10-03 09:28:40 [scrapy] DEBUG: Scraped from <200 http://pitchfork.com/reviews/best/tracks/?page=1>
{'artist': u'Kelly Lee Owens', 'track': u'CBM'}
2016-10-03 09:28:40 [scrapy] DEBUG: Scraped from <200 http://pitchfork.com/reviews/best/tracks/?page=1>
{'artist': u'Sampha', 'track': u'Blood on Me'}
2016-10-03 09:28:40 [scrapy] DEBUG: Scraped from <200 http://pitchfork.com/reviews/best/tracks/?page=1>
{'artist': u'Bon Iver', 'track': u'33 \u2018GOD\u2019'}
2016-10-03 09:28:40 [scrapy] DEBUG: Scraped from <200 http://pitchfork.com/reviews/best/tracks/?page=1>
{'artist': u'Mick Jenkins', 'track': u'Drowning\u201d [ft. BADBADNOTGOOD]'}
2016-10-03 09:28:40 [scrapy] DEBUG: Scraped from <200 http://pitchfork.com/reviews/best/tracks/?page=1>
{'artist': u'Chromatics', 'track': u'Dear Tommy'}
2016-10-03 09:28:40 [scrapy] DEBUG: Scraped from <200 http://pitchfork.com/reviews/best/tracks/?page=1>
{'artist': u'Young Thug', 'track': u'Kanye West'}
2016-10-03 09:28:40 [scrapy] DEBUG: Scraped from <200 http://pitchfork.com/reviews/best/tracks/?page=1>
{'artist': u'Vince Staples', 'track': u'Prima Donna\u201d [ft. A$AP Rocky]'}
2016-10-03 09:28:40 [scrapy] DEBUG: Scraped from <200 http://pitchfork.com/reviews/best/tracks/?page=1>
{'artist': u'Angel Olsen', 'track': u'Sister'}
2016-10-03 09:28:40 [scrapy] DEBUG: Scraped from <200 http://pitchfork.com/reviews/best/tracks/?page=1>
{'artist': u'Cass McCombs', 'track': u'Bum Bum Bum'}
2016-10-03 09:28:40 [scrapy] DEBUG: Scraped from <200 http://pitchfork.com/reviews/best/tracks/?page=1>
{'artist': u'Hamilton Leithauser', 'track': u'A 1000 Times'}
2016-10-03 09:28:40 [scrapy] DEBUG: Scraped from <200 http://pitchfork.com/reviews/best/tracks/?page=1>
{'artist': u'Danny Brown', 'track': u'Pneumonia'}
2016-10-03 09:28:40 [scrapy] DEBUG: Scraped from <200 http://pitchfork.com/reviews/best/tracks/?page=1>
{'artist': u'Frank Ocean', 'track': u'Ivy'}
2016-10-03 09:28:40 [scrapy] DEBUG: Scraped from <200 http://pitchfork.com/reviews/best/tracks/?page=1>
{'artist': u'Frank Ocean', 'track': u'Rushes'}
2016-10-03 09:28:40 [scrapy] DEBUG: Scraped from <200 http://pitchfork.com/reviews/best/tracks/?page=1>
{'artist': u'Half Waif', 'track': u'Turn Me Around'}
2016-10-03 09:28:40 [scrapy] DEBUG: Scraped from <200 http://pitchfork.com/reviews/best/tracks/?page=1>
{'artist': u'Rae Sremmurd', 'track': u'Black Beatles\u201d [ft. Gucci Mane]'}
2016-10-03 09:28:40 [scrapy] DEBUG: Scraped from <200 http://pitchfork.com/reviews/best/tracks/?page=1>
{'artist': u'Bon Iver',
'track': u'22 (OVER S\u221e\u221eN) [Bob Moose Extended Cab Version]'}
2016-10-03 09:28:40 [scrapy] INFO: Closing spider (finished)
2016-10-03 09:28:40 [scrapy] INFO: Dumping Scrapy stats:
{'downloader/request_bytes': 459,
'downloader/request_count': 2,
'downloader/request_method_count/GET': 2,
'downloader/response_bytes': 22624,
'downloader/response_count': 2,
'downloader/response_status_count/200': 2,
'finish_reason': 'finished',
'finish_time': datetime.datetime(2016, 10, 3, 12, 28, 40, 425137),
'item_scraped_count': 23,
'log_count/DEBUG': 26,
'log_count/INFO': 7,
'response_received_count': 2,
'scheduler/dequeued': 1,
'scheduler/dequeued/memory': 1,
'scheduler/enqueued': 1,
'scheduler/enqueued/memory': 1,
'start_time': datetime.datetime(2016, 10, 3, 12, 28, 39, 535638)}
2016-10-03 09:28:40 [scrapy] INFO: Spider closed (finished)
最佳答案
我认为这是因为您的管道从未关闭文件对象。您应该在管道的 open_spider()
方法中打开文件,并在 close_spider()
方法中关闭它。
class PitchforkTracks(object):
def open_spider(self, spider):
self.file = open('tracks.jl', 'wb')
def close_spider(self, spider):
self.file.close()
def process_item(self, item, spider):
line = json.dumps(dict(item)) + "\n"
self.file.write(line)
return item
关于python - Scrapy - 在脚本上下文中打印管道数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39821693/
在一个 scrapy 项目中,人们经常使用中间件。在交互式 session 期间是否也有一种通用方法可以在 scrapy shell 中启用中间件? 最佳答案 尽管如此,在 setting.py 中设
我想对网页中向下滚动生成的内容进行反向工程。问题出在url https://www.crowdfunder.com/user/following_page/80159?user_id=80159&li
我需要帮助将相对URL转换为Scrapy Spider中的绝对URL。 我需要将起始页面上的链接转换为绝对URL,以获取起始页面上已草稿的项目的图像。我尝试使用不同的方法来实现此目标失败,但是我陷入了
我在 Scrapy Python 中制作了一个脚本,它在几个月内一直运行良好(没有更改)。最近,当我在 Windows Powershell 中执行脚本时,它引发了下一个错误: scrapy craw
我已经从 docker 启动了 splash。我为 splash 和 scrapy 创建了大的 lua 脚本,然后它运行我看到了问题: Lua error: error in __gc metamet
我正在使用scrapy 来抓取网站,但发生了不好的事情(断电等)。 我想知道我怎样才能从它坏了的地方继续爬行。我不想从种子开始。 最佳答案 这可以通过将预定的请求持久化到磁盘来完成。 scrapy c
有人可以向我解释一下 Scrapy 中的暂停/恢复功能是如何实现的吗?作品? scrapy的版本我正在使用的是 0.24.5 documentation没有提供太多细节。 我有以下简单的蜘蛛: cla
我想将 apscheduler 与 scrapy.but 我的代码是错误的。 我应该如何修改它? settings = get_project_settings() configure_logging
我正在抓取一个网站并解析一些内容和图像,但即使对于 100 页左右的简单网站,完成这项工作也需要数小时。我正在使用以下设置。任何帮助将不胜感激。我已经看过这个问题- Scrapy 's Scrapyd
我正在抓取一个网站并解析一些内容和图像,但即使对于 100 页左右的简单网站,完成这项工作也需要数小时。我正在使用以下设置。任何帮助将不胜感激。我已经看过这个问题- Scrapy 's Scrapyd
我是爬行新手,想知道是否可以使用 Scrapy 逐步爬行网站,例如 CNBC.com?例如,如果今天我从一个站点抓取所有页面,那么从明天开始我只想收集新发布到该站点的页面,以避免抓取所有旧页面。 感谢
我是scrapy的新手。我正在尝试从 here 下载图像.我在关注 Official-Doc和 this article . 我的 settings.py 看起来像: BOT_NAME = 'shop
我在使用 scrapy 时遇到了一些问题。它没有返回任何结果。我试图将以下蜘蛛复制并粘贴到 scrapy shell 中,它确实有效。真的不确定问题出在哪里,但是当我用“scrapy crawl rx
如何使用 Scrapy 抓取多个 URL? 我是否被迫制作多个爬虫? class TravelSpider(BaseSpider): name = "speedy" allowed_d
当我使用splash渲染整个目标页面来爬取整个网站时出现问题。某些页面不是随机成功的,所以我错误地获取了支持渲染工作完成后出现的信息。这意味着我尽管我可以从其他渲染结果中获取全部信息,但仅从渲染结果中
如何使用 Scrapy 抓取多个 URL? 我是否被迫制作多个爬虫? class TravelSpider(BaseSpider): name = "speedy" allowed_d
我的scrapy程序无论如何只使用一个CPU内核CONCURRENT_REQUESTS我做。 scrapy中的某些方法是否可以在一个scrapy爬虫中使用所有cpu核心? ps:好像有争论max_pr
我最近用 python 和 Selenium 做了一个网络爬虫,我发现它做起来非常简单。该页面使用 ajax 调用来加载数据,最初我等待固定的 time_out 来加载页面。这工作了一段时间。之后,我
我想用这个命令运行 scrapy 服务器: scrapy server 它失败了,因为没有项目。然后我创建一个空项目来运行服务器,并成功部署另一个项目。但是,scrapy 服务器无法处理这个项目,并告
我正在创建一个网络应用程序,用于从不同网站抓取一长串鞋子。这是我的两个单独的 scrapy 脚本: http://store.nike.com/us/en_us/pw/mens-clearance-s
我是一名优秀的程序员,十分优秀!