使用第一个URL的结果Scrapy Scrape多个URL

2024-04-18 19:01:57 发布

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

  1. 我使用Scrapy从第一个URL中收集数据。
  2. 第一个URL返回一个包含URL列表的响应。

到目前为止我还可以。我的问题是,我如何才能进一步刮这个网址列表?在搜索之后,我知道我可以在解析中返回一个请求,但它似乎只能处理一个URL。

这是我的分析:

def parse(self, response):
    # Get the list of URLs, for example:
    list = ["http://a.com", "http://b.com", "http://c.com"]
    return scrapy.Request(list[0])
    # It works, but how can I continue b.com and c.com?

我可以这样做吗?

def parse(self, response):
    # Get the list of URLs, for example:
    list = ["http://a.com", "http://b.com", "http://c.com"]

    for link in list:
        scrapy.Request(link)
        # This is wrong, though I need something like this

完整版本:

import scrapy

class MySpider(scrapy.Spider):
    name = "mySpider"
    allowed_domains = ["x.com"]
    start_urls = ["http://x.com"]

    def parse(self, response):
        # Get the list of URLs, for example:
        list = ["http://a.com", "http://b.com", "http://c.com"]

        for link in list:
            scrapy.Request(link)
            # This is wrong, though I need something like this

Tags: oftheselfcomhttpurlforget