从一个网站刮所有的链接使用刮不工作

2024-10-03 13:18:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图取消所有的链接,这也是在网站上分页。下面给出的是我的代码,但代码不工作。它只是从第一页删除url链接。我该如何取消所有链接?谢谢

# -*- coding: utf-8 -*-
import scrapy


class DummySpider(scrapy.Spider):
    name = 'dummyspider'
    allowed_domains = ['alibaba.com']
    start_urls = ['https://www.alibaba.com/countrysearch/CN/China/products/A.html'
                ]

    def parse(self, response):
        link = response.xpath('//*[@class="column one3"]/a/@href').extract()

        for item in zip(link):
            scraped_info = {
                'link':item[0],

            }
            yield scraped_info
        next_page_url = response.xpath('//*[@class="page_btn"]/@href').extract_first()
        if next_page_url:
            next_page_url = response.urljoin(next_page_url)
            yield scrapy.Request(url = next_page_url, callback = self.parse)

起始url是https://www.alibaba.com/countrysearch/CN/China/products/A.html


Tags: 代码httpscomurl链接responsewwwpage
1条回答
网友
1楼 · 发布于 2024-10-03 13:18:33

您可以通过正确设置起始URL来解决这个问题。你知道吗

string模块具有字母常量:

$ import string
$ string.ascii_uppercase
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

您可以使用以编程方式创建URL:

import string
from scrapy import Spider  

class MySpider(Spider):
    name = 'alibaba'
    start_urls = [
        f'http://foo.com?letter={char}' 
        for char in string.ascii_uppercase
    ]

相关问题 更多 >