【问题标题】:How to print results derived from a loop in scrapy shell?如何打印从scrapy shell中的循环派生的结果?
【发布时间】:2017-09-19 09:58:07
【问题描述】:

我对使用scrapy shell 非常陌生。我搜索了很多但找不到scrapy shell中的打印命令是什么。如果你看看下面的脚本,你就会明白我的意思了。

scrapy shell http://quotes.toscrape.com/ --nolog
for item in response.css(".author"):
    author_name = item.css("::text").extract_first()
    print(author_name)   # What should be the command to print the `author_name`.

所以,如果有人帮助我如何在 scrapy shell 中打印结果,我将不胜感激。谢谢。

【问题讨论】:

  • 你试过pprint吗? from pprint import pprint 然后pprint(author_name)
  • 是的,我试过了。 pprint 不工作。
  • 它对我有用。您使用的是哪个版本的 python 和 Scrapy?您确定 author_name 在您的情况下不是 None 吗?
  • 也适合我。您打算只在屏幕上打印它,对吗?
  • Python 3.5 和 scrapy 1.4

标签: python python-3.x web-scraping scrapy scrapy-spider


【解决方案1】:

当您在交互式 shell 中键入多行表达式时,您应该在最后一行之后按两次 Enter。

查看this answer 以获取有关续行的更多信息。

【讨论】:

  • 感谢 Roman Mindlin 一直陪伴着我。这对我帮助很大。
【解决方案2】:

您可能已经知道这一点,但对于那些不知道的人,这可能会有所帮助:

scrapy shell http://quotes.toscrape.com/ --nolog
for item in response.css(".author"):
    item.css("::text").extract_first()

如果你运行上面的代码,没有 print 语句,这仍然可以给你提取的作者姓名。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-10-06
    • 2017-05-20
    • 2020-09-04
    • 2018-08-12
    • 1970-01-01
    • 1970-01-01
    • 2021-10-12
    • 1970-01-01
    相关资源
    最近更新 更多