如何將Scrapy專案轉換為JSON?
網路抓取是從網站提取資料的過程。它涉及解析HTML或XML程式碼並從中提取相關資訊。Scrapy是一個流行的基於Python的網路抓取框架,允許您輕鬆構建網路抓取程式以從網站提取結構化資料。Scrapy提供了一個強大而高效的框架,用於構建可以從網站提取資料並將其儲存為各種格式的網路爬蟲。
Scrapy的關鍵特性之一是它能夠使用自定義Item類來解析和儲存資料。這些Item類定義了將從網站提取的資料的結構。每個Item類都包含一組與將要提取的資料相對應的欄位。資料提取後,會將其填充到Item類的例項中。
提取資料並填充Item例項後,您可能需要將資料匯出為各種格式以進行進一步分析或儲存。JSON是一種流行的資料格式,它既易於人工閱讀,又易於透過程式設計方式使用。它是一種輕量級文字格式,廣泛用於網路上的資料交換。大多數程式語言都支援JSON,並且它廣泛用於Web應用程式和API。
將Scrapy Item例項轉換為JSON格式是構建網路抓取程式時的常見需求。Scrapy提供了內建方法來將Item例項轉換為JSON格式,但也有一些外部庫可以提供用於處理Python中JSON資料的附加功能。在本文中,我們將探討如何使用Scrapy的內建方法和外部庫將Scrapy Item例項轉換為JSON格式。我們還將討論在Python中使用JSON資料時的一些最佳實踐和常見陷阱。
我們可以採用不同的方法將Scrapy專案轉換為JSON。
方法一:使用Scrapy內建的JSON匯出器
Scrapy提供了一個內建的JSON匯出器,可用於將Scrapy Item例項轉換為JSON格式。您可以使用scrapy.exporters.JsonItemExporter類將您的專案匯出到JSON檔案。
請考慮以下程式碼。
示例
import scrapy
from scrapy.exporters import JsonItemExporter
class MySpider(scrapy.Spider):
name = 'myspider'
start_urls = ['http://www.example.com']
def parse(self, response):
item = {
'title': response.css('title::text').get(),
'description': response.css('meta[name="description"]::attr(content)').get()
}
yield item
def closed(self, reason):
items = list(self.crawler.stats.get_value('item_scraped_count').values())[0]
filename = 'data.json'
with open(filename, 'wb') as file:
exporter = JsonItemExporter(file)
exporter.start_exporting()
for item in self.crawler.stats.get_value('items'):
exporter.export_item(item)
exporter.finish_exporting()
self.log(f'Saved file {filename}, containing {items} items')
解釋
我們匯入必要的模組:scrapy用於構建spider,JsonItemExporter用於將專案匯出到JSON。
我們定義了一個名為MySpider的新spider,它使用CSS選擇器從網站提取標題和描述,並將它們儲存在一個名為item的字典中。
我們將item字典傳遞給Scrapy,Scrapy會自動將其填充到scrapy.Item類的例項中。
spider完成網站抓取後,將呼叫closed方法。在這個方法中,我們檢索spider抓取的專案,並使用JsonItemExporter將其儲存到JSON檔案中。
執行spider時,它將從網站提取標題和描述,並將結果儲存到名為data.json的JSON檔案中。
輸出
[{ "title": "Example Domain", "description": "Example Domain. This domain is for use in illustrative examples in documents. You may use this domain in literature without prior coordination or asking for permission."}]
方法二:使用Python內建JSON
示例
import scrapy
import json
class MySpider(scrapy.Spider):
name = 'myspider'
start_urls = ['http://www.example.com']
def parse(self, response):
item = {
'title': response.css('title::text').get(),
'description': response.css('meta[name="description"]::attr(content)').get()
}
yield item
def closed(self, reason):
items = list(self.crawler.stats.get_value('item_scraped_count').values())[0]
filename = 'data.json'
with open(filename, 'w') as file:
json.dump(self.crawler.stats.get_value('items'), file, indent=4)
self.log(f'Saved file {filename}, containing {items} items')
輸出
[{ "title": "Example Domain", "description": "Example Domain. This domain is for use in illustrative examples in documents. You may use this domain in literature without prior coordination or asking for permission."}]
結論
總之,Scrapy是一個強大的網路爬取和抓取框架,允許您以結構化的方式從網站提取資料。
在本文中,我們探討了兩種將Scrapy Item例項轉換為JSON的不同方法。第一種方法涉及使用Scrapy提供的JsonItemExporter類。
資料結構
網路
關係資料庫管理系統(RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP