首页 > 解决方案 > 试图从网络上获取链接

问题描述

该代码仅获得 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,
    }

标签: pythonxpathhyperlinkscrapyhref

解决方案


您的缩进不正确(您需要yieldfor循环内移动):

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,}

推荐阅读