gpt4 book ai didi

python 2 从urllib2响应结果中获取汉字之间的数字

转载 作者:行者123 更新时间:2023-12-01 02:53:48 25 4
gpt4 key购买 nike

我想使用 urllib2 从网页中获取 html 标记之间的数字 805。

<span class="count">(共805张)</span>

这是我编写的用于获取号码的 python 代码:

url = "https://movie.douban.com/celebrity/1044996/photos/"
request = urllib2.Request(url,headers=headers)
response = urllib2.urlopen(request)
content = response.read().decode('utf-8')
pattern1 = re.compile(r'<span\sclass="count">(.*?)</', re.S)
result1 = re.search(pattern1, content)
total_num = result1.group(1)
total_num = total_num

但是当我打印total_num时,控制台显示:

u'(\u5171805\u5f20)'

如何使用正则表达式获取期望的数字 805?

最佳答案

如果你的html标签总是这样的形式:

<span class="count">(共805张)</span>

这意味着数字位于两个非拉丁字符之间,并且 '('')' 您可以使用此模式:

import re
a = <span class="count">(共805张)</span>
# This will work if theString is unicode,
# or a string in an encoding where ASCII
# occupies values 0 to 0x7F (latin-1, UTF-8, etc.)
final = re.findall('\([^\x00-\x7F]+(\d+)[^\x00-\x7F]+\)', a)

print final

输出:

['805']

PS:归功于此 asnwer经过一些修改。

关于python 2 从urllib2响应结果中获取汉字之间的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44451187/

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