Python Forum
Extract json-ld schema markup data and store in MongoDB
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Extract json-ld schema markup data and store in MongoDB
#1
I'am creating a spider to crawl webpage' json-ld schema markup and store data in mongodb. actually I want to scrape json-ld schema markup and extract the data type("@type" : "_____") from schema markup and store this @type in mongodb. My spiders crawl well whole schema markup code. But I want to know that How to extract @type from that json-ld schema markup and store it in mongodb.
This is my spider files

apple_spider.py

import scrapy
from pprint import pprint
from extruct.jsonld import JsonLdExtractor
from ..items import ApplespiderItem

class AppleSpider(scrapy.Spider):
    name = 'apple'
    allowed_domains = ['apple.com']
    start_urls = (
        'http://www.apple.com/shop/mac/mac-accessories',
        )

    def parse(self, response):

        extractor = JsonLdExtractor()

        items = extractor.extract(response.body_as_unicode(), response.url)
        pprint(items)

        for item in items:
            if item.get('properties', {}).get('name'):
                properties = item['properties']

                
                yield {
                    'name': properties['name'],
                    'price': properties['offers']['properties']['price'],
                    'url': properties['url']
                }
items.py

import scrapy


class ApplespiderItem(scrapy.Item):
    # define the fields for your item here like:
    name = scrapy.Field()
    price = scrapy.Field()
    url = scrapy.Field()
pipelines.py

import pymongo

class ApplespiderPipeline(object):

	def __init__(self):
		self.conn = pymongo.MongoClient(
			'localhost',
			27017
		)
		db = self.conn['newdb']
		self.collection = db['app_tb']

	def process_item(self, item, spider):
		self.collection.insert(dict(item))
		return item
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Extract data from sports betting sites nestor 3 2,793 Mar-30-2021, 04:37 PM
Last Post: Larz60+
  Retrieve images base64 encoded MongoDB and Flask Nuwan16 2 1,503 Oct-13-2020, 06:25 PM
Last Post: Nuwan16
  How and where to store a data for path tree? zayacEBN 1 841 Aug-21-2020, 10:14 PM
Last Post: Larz60+
  Store Screenshot Selenium + MongoDB Nuwan16 9 1,476 Aug-18-2020, 03:57 AM
Last Post: ndc85430
  Extract data from a table Bob_M 3 1,102 Aug-14-2020, 03:36 PM
Last Post: Bob_M
  filtering by category flask+mongodb Leon79 3 1,148 Jul-19-2020, 04:25 AM
Last Post: ndc85430
  error when trying to update mongodb damian0612 6 1,351 Jul-04-2020, 07:25 PM
Last Post: damian0612
  Extract data with Selenium and BeautifulSoup nestor 3 1,717 Jun-06-2020, 01:34 AM
Last Post: Larz60+
  Extract data from a webpage cycloneseb 5 1,454 Apr-04-2020, 10:17 AM
Last Post: alekson
  How to crawl schema markup data type using scrapy? Nuwan16 1 1,282 Mar-31-2020, 03:42 PM
Last Post: stranac

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020