Python 正则表达式 re 中出现'str' object has no attribute 'findall'

2018-08-03 19:52:13 +08:00
 h19981126g
Python 正则表达式 re 中出现'str' object has no attribute 'findall',尝试了很多次,就是不行!
无论是 findall 还是 match 还是 search 都是'str'不行,.text 和.content 也都试过,总是报错。麻烦大神们帮个忙!
代码如下

import requests
import re
proxies = {"http": "http://222.190.126.62:808"}
r = requests.get('http://icanhazip.com/', proxies=proxies).text
re = '^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$'
ip = re.findall(re, r)
print(ip)
12121 次点击
所在节点    Python
5 条回复
AX5N
2018-08-03 20:00:58 +08:00
re 被占用了
kenorizon
2018-08-03 20:02:53 +08:00
第 5 行给局部变量 re 赋值的时候把第二行 import 的 re 模块覆盖掉了
所以第 6 行 re.findall 实际上尝试执行第 5 行的字符串的 findall 方法
把正则字符串的变量换成别的名字就好
TuringGunner
2018-08-03 20:03:04 +08:00
你变量名非要叫 re 么……
wsh1108
2018-08-03 20:11:41 +08:00
一般都叫 pattern 吧
h19981126g
2018-08-03 20:14:37 +08:00
@AX5N 好的吧
@kenorizon
@TuringGunner 知道了,谢谢!

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/476712

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX