Scrapy 提取 ld+JSON

Ric*_*ick 3 python xpath json scrapy

如何提取名称和网址?

报价_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
Run Code Online (Sandbox Code Playgroud)

要提取的数据

<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>
Run Code Online (Sandbox Code Playgroud)

Pet*_*ite 6

这行代码返回一个包含你想要的数据的字典:

data = json.loads(response.xpath('//script[@type="application/ld+json"]//text()').extract_first())
Run Code Online (Sandbox Code Playgroud)

您需要做的就是像这样访问它:

name = data['itemListElement'][0]['name']
url = data['itemListElement'][0]['url']
Run Code Online (Sandbox Code Playgroud)

鉴于微数据包含一个列表,您需要检查您是否指的是列表中的正确产品。