如何提取名称和url?
quotes_spiders.py
import scrapy
import json
class QuotesSpider(scrapy.Spider):
name = "quotes"
start_urls = ["http://www.lazada.com.my/shop-power-banks2/?price=1572-1572"]
def parse(self, response):
data = json.loads(response.xpath('//script[@type="application/ld+json"]//text()').extract_first())
//how to extract the name and url?
yield data
要提取的数据
<script type="application/ld+json">{"@context":"https://schema.org","@type":"ItemList","itemListElement":[{"@type":"Product","image":"http://my-live-02.slatic.net/p/2/test-product-0601-7378-08684315-8be741b9107b9ace2f2fe68d9c9fd61a-webp-catalog_233.jpg","name":"test product 0601","offers":{"@type":"Offer","availability":"https://schema.org/InStock","price":"99999.00","priceCurrency":"RM"},"url":"http://www.lazada.com.my/test-product-0601-51348680.html?ff=1"}]}</script>
最佳答案
这行代码返回一个包含您想要的数据的字典:
data = json.loads(response.xpath('//script[@type="application/ld+json"]//text()').extract_first())
您需要做的就是像这样访问它:
name = data['itemListElement'][0]['name']
url = data['itemListElement'][0]['url']
鉴于微数据包含一个列表,您需要检查您指的是列表中的正确产品。
关于python - Scrapy 提取ld+JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44939247/