【问题标题】:Trying to get links from web试图从网络上获取链接
【发布时间】:2019-11-30 17:31:39
【问题描述】:

代码仅获得 1 个 URL 列表然后停止。它应该得到209个项目。为什么只打印一行?

def parse_link(self, response):

    # Extract the list of products 
    results = response.xpath('//li[@class="s-item   "]')



    # Extract info for each product
    for product in results:
        name = product.xpath('//*[@class="s-item__link"]').extract_first()[30:124]

    yield{
    "Name":name,
    }

【问题讨论】:

  • 没有任何其他信息很难分辨。请分享所有相关代码和数据。请参阅:minimal reproducible example
  • 看起来您正在使用作为生成器语句的 yield。你是通过 for 循环调用这个函数吗?

标签: python xpath hyperlink scrapy href


【解决方案1】:

您的缩进不正确(您需要将yield 移动到for 循环内):

def parse_link(self, response):

    # Extract the list of products 
    results = response.xpath('//li[@class="s-item   "]')



    # Extract info for each product
    for product in results:
        name = product.xpath('//*[@class="s-item__link"]').extract_first()[30:124]

        yield {"Name":name,}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-23
    • 2013-07-22
    • 1970-01-01
    • 2020-01-25
    • 2013-03-14
    • 1970-01-01
    • 2021-08-25
    • 2020-08-05
    相关资源
    最近更新 更多