Scrapy 具有多个页面

Ran*_*dan 2 scrapy web-scraping python-3.x

我创建了一个简单的 scrapy 项目,在其中,我从初始站点 example.com/full 获取了总页码。现在我需要抓取从 example.com/page-2 开始到 100 的所有页面(如果总页数为 100)。我怎样才能做到这一点?

任何意见将是有益的。

代码:

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

更新#1:

我尝试使用它,urls = ('https://example.com/page-{}'.format(i) for i in range(1,total_pages))但它不起作用,可能是我做错了什么。

更新#2:我已经像这样更改了我的代码

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

但仍然循环重复只显示第一页标题。我需要从不同页面中提取标题并在提示中打印它。我怎样才能做到这一点?

小智 5

您必须搜索“next_page”对象并在该对象位于页面上时继续循环。

带有 next_page 元素的 img

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

如果您使用“-o sanet.json”键运行此代码,您将得到以下结果。

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