简体   繁体   中英

Scrapy How to scrape items from multiple pages?

I am trying to scrape data of # pages. I have already done a scraper which can scrape data from a single # page. But it suddenly finished the work after scraping of the first page

The whole file with parse function and scrapd function - Scraper.py

# -*- coding: utf-8 -*-
import scrapy
import csv
import os
from scrapy.selector import Selector
from scrapy import Request

class Proddduct(scrapy.Item):
    price = scrapy.Field()
    description = scrapy.Field()
    link = scrapy.Field()
    content = scrapy.Field()


class LapadaScraperSpider(scrapy.Spider):
    name = 'lapada_scraper2'
    allowed_domains = ['http://www.lapada.org']
    start_urls = ['https://lapada.org/art-and-antiques/?search=antique']

    def parse(self, response):
        next_page_url = response.xpath("//ul/li[@class='next']//a/@href").get()

        for item in self.scrape(response):
            yield item

        if next_page_url:
            print("Found url: {}".format(next_page_url))
            yield scrapy.Request(url=next_page_url, callback=self.parse)

    def scrape(self, response):
        parser = scrapy.Selector(response)

        products = parser.xpath("//div[@class='content']")

        for product in products:
            item = Proddduct()
            XPATH_PRODUCT_DESCRIPTION  = ".//strong/text()"
            XPATH_PRODUCT_PRICE  = ".//div[@class='price']/text()"
            XPATH_PRODUCT_LINK = ".//a/@href"

            raw_product_description = product.xpath(XPATH_PRODUCT_DESCRIPTION).extract()
            raw_product_price = product.xpath(XPATH_PRODUCT_PRICE).extract()
            raw_product_link = product.xpath(XPATH_PRODUCT_LINK).extract_first()

            item['description'] = raw_product_description
            item['price'] = raw_product_price
            item['link'] = raw_product_link

            yield item

    def get_information(self, response):
        item = response.meta['item'] 
        item['phonenumber'] = "12345"
        yield item

How can I scrape all items in all pages?

Thanks

Change allowed_domains = ['http://www.lapada.org'] to allowed_domains = ['lapada.org']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM