gpt4 book ai didi

regex - 使用正则表达式从页面文本中提取数字

转载 作者:行者123 更新时间:2023-12-01 23:50:45 25 4
gpt4 key购买 nike

我一直在寻找关于如何搜索页面上的所有文本以及如果它与正则表达式匹配然后提取它的几个小时。我的蜘蛛设置如下:

def parse(self, response):
title = response.xpath('//title/text()').extract()
units = response.xpath('//body/text()').re(r"Units: (\d)")
print title, units

我想把页面上“单位:”后面的数字拉出来。当我在带有 Units: 351 的页面上运行 scrapy 时,我只会得到页面的标题,前后有一堆转义符,而没有任何单位。

我是scrapy的新手并且有一点python经验。任何有关如何在 Units: 之后提取整数并从标题中删除额外转义字符“u'\r\n\t...”的帮助将不胜感激。

编辑:
根据评论,这里是示例页面的部分 html 摘录。请注意,在此示例中,除了 p 之外,这可能位于不同的标签内:
<body>
<div> Some content and multiple Divs here <div>
<h1>This is the count for Dala</h1>
<p><strong>Number of Units:</strong> 801</p>
<p>We will have other content here and more divs beyond</p>
</body>

根据下面的答案,这就是大部分情况。仍在努力删除 Units: 和额外的转义字符。
units = response.xpath('string(//body)').re("(Units: [\d]+)")

最佳答案

尝试:

response.xpath('string(//body)').re(r"Units: (\d)")

关于regex - 使用正则表达式从页面文本中提取数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26723378/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com