【发布时间】:2018-09-13 02:43:10
【问题描述】:
关于 Scrapy 中 json 导出格式的快速问题。我导出的文件如下所示。
{"pages": {"title": "x", "text": "x", "tags": "x", "url": "x"}}
{"pages": {"title": "x", "text": "x", "tags": "x", "url": "x"}}
{"pages": {"title": "x", "text": "x", "tags": "x", "url": "x"}}
但我希望它采用这种精确的格式。不知何故,我需要在“页面”下获取所有其他信息。
{"pages": [
{"title": "x", "text": "x", "tags": "x", "url": "x"},
{"title": "x", "text": "x", "tags": "x", "url": "x"},
{"title": "x", "text": "x", "tags": "x", "url": "x"}
]}
我在scrapy 或python 方面不是很有经验,但除了导出格式之外,我已经在我的蜘蛛中完成了所有其他工作。这是我刚刚开始工作的 pipelines.py。
from scrapy.exporters import JsonItemExporter
import json
class RautahakuPipeline(object):
def open_spider(self, spider):
self.file = open('items.json', 'w')
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
这些是我需要提取的 spider.py 中的项目
items = []
for title, text, tags, url in zip(product_title, product_text, product_tags, product_url):
item = TechbbsItem()
item['pages'] = {}
item['pages']['title'] = title
item['pages']['text'] = text
item['pages']['tags'] = tags
item['pages']['url'] = url
items.append(item)
return items
非常感谢任何帮助,因为这是我项目中的最后一个障碍。
编辑
items = {'pages':[{'title':title,'text':text,'tags':tags,'url':url}
for title, text, tags, url in zip(product_title, product_text, product_tags, product_url)]}
这会以这种格式提取 .json
{"pages": [{"title": "x", "text": "x", "tags": "x", "url": "x"}]}
{"pages": [{"title": "x", "text": "x", "tags": "x", "url": "x"}]}
{"pages": [{"title": "x", "text": "x", "tags": "x", "url": "x"}]}
这越来越好,但我仍然需要文件开头的一个“页面”以及它下面的数组中的所有其他内容。
编辑 2
我认为我的 spider.py 是“pages”被添加到 .json 文件中每一行的原因,我最初应该发布它的整个代码。在这里。
# -*- coding: utf-8 -*-
import scrapy
from urllib.parse import urljoin
class TechbbsItem(scrapy.Item):
pages = scrapy.Field()
title = scrapy.Field()
text= scrapy.Field()
tags= scrapy.Field()
url = scrapy.Field()
class TechbbsSpider(scrapy.Spider):
name = 'techbbs'
allowed_domains = ['bbs.io-tech.fi']
start_urls = ['https://bbs.io-tech.fi/forums/prosessorit-emolevyt-ja-muistit.73/?prefix_id=1' #This is a list page full of used pc-part listings
]
def parse(self, response): #This visits product links in the product list page
links = response.css('a.PreviewTooltip::attr(href)').extract()
for l in links:
url = response.urljoin(l)
yield scrapy.Request(url, callback=self.parse_product)
next_page_url = response.xpath('//a[contains(.,"Seuraava ")]/@href').extract_first()
if next_page_url:
next_page_url = response.urljoin(next_page_url)
yield scrapy.Request(url=next_page_url, callback=self.parse)
def parse_product(self, response): #This extracts data from inside the links
product_title = response.xpath('normalize-space(//h1/span/following-sibling::text())').extract()
product_text = response.xpath('//b[contains(.,"Hinta:")]/following-sibling::text()[1]').re('([0-9]+)')
tags = "tags" #This is just a placeholder
product_tags = tags
product_url = response.xpath('//html/head/link[7]/@href').extract()
items = []
for title, text, tags, url in zip(product_title, product_text, product_tags, product_url):
item = TechbbsItem()
item['pages'] = {}
item['pages']['title'] = title
item['pages']['text'] = text
item['pages']['tags'] = tags
item['pages']['url'] = url
items.append(item)
return items
所以我的蜘蛛开始从一个充满产品列表的页面爬行。它访问 50 个产品链接中的每一个,并抓取 4 个项目、标题、文本、标签和 url。在抓取一页中的每个链接后,它会转到下一个,依此类推。我怀疑代码中的循环会阻止您的建议对我有用。
我想将 .json 导出为原始问题中提到的确切形式。 Se 在文件的开头会有{"pages": [,然后是所有缩进的项目行
{"title": "x", "text": "x", "tags": "x", "url": "x"},,最后是]}
【问题讨论】:
标签: python json scrapy export scrapy-pipeline