import requests, re r = requests.get('example.com') p = re.compile('\d') print(p.match(str(r.text)))
这总是打印 None,即使 r.text 肯定包含数字,但 print(p.match('12345')) 有效。我需要对 r.text 做什么才能使其可以被 re.compile.match() 读取?转换为 str 显然是不够的。
这是因为
re.match
r.text
如果您想查找第一个匹配项,请使用
re.search
import requests, re r = requests.get('https://example.com') p = re.compile(r'\d') print(p.search(r.text))
输出:
<re.Match object; span=(88, 89), match='8'>