【问题标题】:SCRAPY not printing all the items in the terminalSCAPY 不打印终端中的所有项目
【发布时间】:2020-09-17 12:37:59
【问题描述】:

我正在使用 SCRAPY Shell 在终端中使用此命令,但它没有打印所有项目。

scrapy shell https://access.redhat.com/errata/RHSA-2017:0621

response.xpath('normalize-space((//div[contains(@class, "tab-pane")]/ul)[2]/li/text())').getall()

它只打印第一项。

【问题讨论】:

  • 您的 xpath 仅匹配该页面上的一个元素。您期望得到的结果是什么?
  • XPath 显示 8 个项目中的 1 个。我想得到所有 8 件物品。

标签: python scrapy scrapy-splash scrapy-shell


【解决方案1】:

normalize-space() 仅适用于单个节点。如果你给它一个节点集,它将返回从第一个节点产生的值。

如果你想将它应用到多个节点,你可以这样做(pp只是一个漂亮的打印功能):

>>> products = response.xpath('(//div[contains(@class, "tab-pane")]/ul)[2]/li').xpath('normalize-space()').getall()
>>> pp(products)
[
    'Red Hat Enterprise Linux Server 6 x86_64',
    'Red Hat Enterprise Linux Server 6 i386',
    'Red Hat Enterprise Linux Workstation 6 x86_64',
    'Red Hat Enterprise Linux Workstation 6 i386',
    'Red Hat Enterprise Linux Desktop 6 x86_64',
    'Red Hat Enterprise Linux Desktop 6 i386',
    'Red Hat Enterprise Linux for Power, big endian 6 ppc64',
    'Red Hat Enterprise Linux for Scientific Computing 6 x86_64'
]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-04
    • 2012-12-11
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多