【问题标题】:Scrapy item result inside an html code在 html 代码中抓取项目结果
【发布时间】:2018-04-04 16:26:17
【问题描述】:

是否可以将抓取项目的结果插入到 html 代码中?如何实现?

类似这样的:

for ntp in response.css('div.content-1col-nobox'):
description = ntp.xpath('normalize-space(//div[6]/div[1]/div[2]/div/blockquote[2]/p/text())').extract_first()
'Description': '<html><body>......'description'..............</body></html>',\

提前谢谢你。

【问题讨论】:

  • 是的,这是可能的,我建议您在正则表达式中记录自己以及它们如何帮助您将描述字符串放置在 html 代码的所需位置
  • 谢谢@ClementLombard 我会做的

标签: python html xpath scrapy


【解决方案1】:

您可以使用简单的字符串操作。 1.'%d' % (description)

  1. '&lt;html&gt;{} some more html &lt;/html&gt;'.format(description)

您需要将您的描述变量设为字符串格式。 我猜 str(description) 会起作用。

description = ntp.xpath('normalize-space(//div[6]/div[1]/div[2]/div/blockquote[2]/p/text())').extract_first()

'Description': '<html><body>...... {} ..............</body></html>'.format(str(descripion))

按照评论中的建议,您可以使用 python RE https://docs.python.org/3/library/re.html

【讨论】:

  • 谢谢@AshishRp 它工作得很好,就像我看到的那样。再次感谢您
猜你喜欢
  • 1970-01-01
  • 2015-04-08
  • 1970-01-01
  • 2013-12-31
  • 2014-10-04
  • 1970-01-01
  • 1970-01-01
  • 2021-03-08
  • 2021-10-10
相关资源
最近更新 更多