V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
h19981126g
V2EX  ›  Python

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

  •  
  •   h19981126g · Aug 3, 2018 · 12770 views
    This topic created in 2828 days ago, the information mentioned may be changed or developed.
    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)
    5 replies    2018-08-03 20:14:37 +08:00
    AX5N
        1
    AX5N  
       Aug 3, 2018   ❤️ 1
    re 被占用了
    kenorizon
        2
    kenorizon  
       Aug 3, 2018   ❤️ 1
    第 5 行给局部变量 re 赋值的时候把第二行 import 的 re 模块覆盖掉了
    所以第 6 行 re.findall 实际上尝试执行第 5 行的字符串的 findall 方法
    把正则字符串的变量换成别的名字就好
    TuringGooner
        3
    TuringGooner  
       Aug 3, 2018 via iPhone
    你变量名非要叫 re 么……
    wsh1108
        4
    wsh1108  
       Aug 3, 2018 via Android
    一般都叫 pattern 吧
    h19981126g
        5
    h19981126g  
    OP
       Aug 3, 2018
    @AX5N 好的吧
    @kenorizon
    @TuringGunner 知道了,谢谢!
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   813 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 64ms · UTC 20:25 · PVG 04:25 · LAX 13:25 · JFK 16:25
    ♥ Do have faith in what you're doing.