Jan*_*lmi 2 python json export scrapy scrapy-pipeline
只是一个关于 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"}}
Run Code Online (Sandbox Code Playgroud)
但我希望它采用这种确切的格式。不知何故,我需要在“页面”下获取所有其他信息。
{"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"}
]}
Run Code Online (Sandbox Code Playgroud)
我在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
Run Code Online (Sandbox Code Playgroud)
这些是我需要提取的 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
Run Code Online (Sandbox Code Playgroud)
非常感谢任何帮助,因为这是我项目中的最后一个障碍。
编辑
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 是将“页面”添加到 .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
Run Code Online (Sandbox Code Playgroud)
所以我的蜘蛛开始从一个充满产品列表的页面爬行。它访问 50 个产品链接中的每一个,并抓取 4 个项目,标题、文本、标签和 URL。抓取一页中的每个链接后,它会转到下一个,依此类推。我怀疑代码中的循环阻止了您的建议对我有用。
我想将 .json 导出到原始问题中提到的确切形式。Se 将{"pages": [在文件的开头,然后是所有缩进的项目行
{"title": "x", "text": "x", "tags": "x", "url": "x"},,最后是]}
在内存使用方面,这不是一个好习惯,但一个选择是保留一个对象并在进程结束时写入它:
class RautahakuPipeline(object):
def open_spider(self, spider):
self.items = { "pages":[] }
self.file = null # open('items.json', 'w')
def close_spider(self, spider):
self.file = open('items.json', 'w')
self.file.write(json.dumps(self.items))
self.file.close()
def process_item(self, item, spider):
self.items["pages"].append(dict(item))
return item
Run Code Online (Sandbox Code Playgroud)
然后,如果内存有问题(无论如何都必须注意),请尝试按如下方式编写 json 文件:
class RautahakuPipeline(object):
def open_spider(self, spider):
self.file = open('items.json', 'w')
header='{"pages": ['
self.file.write(header)
def close_spider(self, spider):
footer=']}'
self.file.write(footer)
self.file.close()
def process_item(self, item, spider):
line = json.dumps(dict(item)) + "\n"
self.file.write(line)
return item
Run Code Online (Sandbox Code Playgroud)
我希望它有帮助。