Ranjith Varatharajan
Ranjith Varatharajan

Reputation: 1694

Scrapy with multiple pages

I have created a simple scrapy project, In which, I got the total page number from the initial site example.com/full. Now I need to scrape all the page starting from example.com/page-2 to 100(if total page count is 100). How can I do that?

Any advice would be helpful.

Code:

import scrapy


class AllSpider(scrapy.Spider):
    name = 'all'
    allowed_domains = ['example.com']
    start_urls = ['https://example.com/full/']
    total_pages = 0

def parse(self, response):
    total_pages = response.xpath("//body/section/div/section/div/div/ul/li[6]/a/text()").extract_first()
    #urls = ('https://example.com/page-{}'.format(i) for i in range(1,total_pages))
    print(total_pages)

Update #1:

I tried using that urls = ('https://example.com/page-{}'.format(i) for i in range(1,total_pages)) but its not working, may be i'm doing something wrong.

Update #2: I have changed my code like this one

class AllSpider(scrapy.Spider):
name = 'all'
allowed_domains = ['sanet.st']
start_urls = ['https://sanet.st/full/']
total_pages = 0

def parse(self, response):
    total_pages = response.xpath("//body/section/div/section/div/div/ul/li[6]/a/text()").extract_first()
    for page in range(2, int(total_pages)):
        url = 'https://sanet.st/page-'+str(page)
        yield scrapy.Request(url)
        title =  response.xpath('//*[@class="list_item_title"]/h2/a/span/text()').extract()
        print(title)

But still the loop showing only the first page title repeatedly. I need to extract the title from different pages and print it in the prompt. How can i do that?

Upvotes: 2

Views: 10242

Answers (3)

soldy
soldy

Reputation: 366

You must search for the 'next_page' object and continue to loop while it is on the page.

img with next_page element

# -*- coding: utf-8 -*-
import scrapy
from scrapy.http import Request


class SanetSpider(scrapy.Spider):
    name = 'sanet'
    allowed_domains = ['sanet.st']
    start_urls = ['https://sanet.st/full/']

    def parse(self, response):
        yield {
            # Do something.
            'result': response.xpath('//h3[@class="posts-results"]/text()').extract_first()
        }

        # next_page = /page-{}/ where {} number of page.
        next_page = response.xpath('//a[@data-tip="Next page"]/@href').extract_first()

        # next_page = https://sanet.st/page-{}/ where {} number of page.
        next_page = response.urljoin(next_page)

        # If next_page have value
        if next_page:
            # Recall parse with url https://sanet.st/page-{}/ where {} number of page.
            yield scrapy.Request(url=next_page, callback=self.parse)

If you run this code with the "-o sanet.json" key you will get the following result.

scrapy runspider sanet.py -o sanet.json

[
{"result": "results 1 - 15 from 651"},
{"result": "results 16 - 30 from 651"},
{"result": "results 31 - 45 from 651"},
...
etc.
...
{"result": "results 631 - 645 from 651"},
{"result": "results 646 - 651 from 651"}
]

Upvotes: 5

Kevin He
Kevin He

Reputation: 1250

an alternative way as shown in the tutorial is to use yield response.follow(url, callback=self.parse_page) and it supports relative URLs directly.

Upvotes: 0

Danil
Danil

Reputation: 5171

from scrapy.http import Request


def parse(self, response):
    total_pages = response.xpath("//body/section/div/section/div/div/ul/li[6]/a/text()").extract_first()
    urls = ('https://example.com/page-{}'.format(i) for i in range(1,total_pages))
    for url in urls:
        yield Request(url, callback=self.parse_page)

def parse_page(self, response):
    # do the stuff

Upvotes: 1

Related Questions