【问题标题】:How to store scrapy shell output/response to variable instead of html file如何将scrapy shell输出/响应存储到变量而不是html文件
【发布时间】:2019-10-03 16:42:09
【问题描述】:

我正在尝试使用 cmdline.execute 将 html 代码存储到名为 response 的变量中,如下面的代码所示,但是它无法在 scrapy shell 中存储和编程代码中断,谁能告诉我如何将原始 html 存储到变量

导入scrapy

从 scrapy 导入命令行

linkedinnurl = "https://stackoverflow.com/users/5597065/adnan-stab=profile"

response = cmdline.execute("scrapy shell https://stackoverflow.com/users/5597065/adnan-s?tab=profile".split()))

打印(响应)

【问题讨论】:

  • @vezunchik 显然不是重复的。链接问题旨在存储requests.post 的值,而此问题旨在存储cmdline.execute 发起的操作的结果。完全不同的场景。
  • 嗯,是的,我的错。谢谢。

标签: web-scraping scrapy


【解决方案1】:

您可以这样做将原始 html 存储到变量中:

 class MySpider(scrapy.Spider):
        def parse(self, res):
            with open(dynamic_file_name_function(res.url), 'w') as f:
                f.write(res.body)

如果您不需要动态文件名,那么只需:

 class MySpider(scrapy.Spider):
        def parse(self, res):
            with open(your_file_path, 'w') as f:
                f.write(res.body)

【讨论】:

  • 这是一个创建动态文件名的函数。如果你不需要动态文件名,你可以删除它。我已经更新了答案
猜你喜欢
  • 2018-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-02
相关资源
最近更新 更多