python - Scrapy只返回了1个项目,我的大脑卡住了

标签 python scrapy

这是蜘蛛.py:

import scrapy
from scrapy.loader import ItemLoader
from dts.items import DtItem

class dtSpider(scrapy.Spider):
    name = 'dts'
    urls = ['s','s','s','s']

    def start_requests(self):
        for url in self.urls:
            yield scrapy.Request(url, callback=self.parse)

    def parse(self,response):
        for title in response.xpath('//h2/a/@title').extract()[:-6]:
            url_array = response.url.split('/')
            author = url_array[url_array.index('author')+1]
            l = ItemLoader(item=DtItem(), response=response)
            l.add_value('title',title)
            l.add_value('author',author)
            return l.load_item()

我写这篇文章只是为了获取标题。 但是,为什么我无法获得所有头衔呢? Scrapy 每页仅返回 1 个项目。

最佳答案

更改:

return l.load_item()

至:

yield l.load_item()

return 将停止该函数,但 yield 将继续运行。

关于python - Scrapy只返回了1个项目,我的大脑卡住了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42761915/

相关文章:

python - 在 mac 上安装 scrapy - 找不到 pyasn1 发行版

python - 在 scrapy 中使用 Tor 代理

python - 给定 'anchor' 和 'reach' 的所有范围组合

java - 为什么 python 没有简单的安装命令(如 mvn install)?

python - 在 bash 或 python 中有没有一种方法可以从一个脚本中获取输出并使用另一个脚本与之交互?

python - scrapy的Request函数没有被调用

python - 如何使用scrapy解析xml

python - 寻求理解 Scrapy 回调

python - 将数组中的某些元素相乘

python - Python 中的 HTTP 代理服务器(带身份验证)