V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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
zhongshaohua
V2EX  ›  Python

新手请教正则提取

  •  
  •   zhongshaohua · 2016-08-01 09:20:52 +08:00 · 3018 次点击
    这是一个创建于 2825 天前的主题,其中的信息可能已经有所发展或是发生改变。
    <img class="BDE_Image" src="http://imgsrc.baidu.com/forum/w%3D580/sign=2e8f3ca53af33a879e6d0012f65d1018/4ece3bc
    79f3df8dc2ab63004cd11728b46102899.jpg" width="560" height="400" changedsize="true">
    想提取 src 中的链接 reg = r'src="(.*?\.jpg)" width'
    为什么 r'src="(.*?\.jpg)"' 和 r'src="(.*\.jpg)" width' 这样不行?
    14 条回复    2016-08-05 14:05:29 +08:00
    zhongshaohua
        1
    zhongshaohua  
    OP
       2016-08-01 09:24:33 +08:00
    用的是 findall
    laoyur
        2
    laoyur  
       2016-08-01 09:26:48 +08:00
    源码中有换行,你得使用 single line 模式
    lxy
        3
    lxy  
       2016-08-01 09:34:23 +08:00
    加 re.S
    或者用这个: r'src="([^"]+)"'
    zhongshaohua
        4
    zhongshaohua  
    OP
       2016-08-01 10:30:28 +08:00
    @lxy 谢谢 但是你这样为什么没有匹配引号呢? 因为你是^" 这样是匹配引号开头吧?
    CosimoZi
        5
    CosimoZi  
       2016-08-01 10:44:52 +08:00
    不要用正则匹配 html
    linesh
        6
    linesh  
       2016-08-01 10:51:49 +08:00
    我在这里试了一下你的表达式,是可以的: http://regexr.com/

    所以可能的原因有:
    * 是否忘记了加 g 参数表示 global ,匹配多行?
    * 是否可能是 Python 正则有其方言或者独特语法?
    lllooo
        7
    lllooo  
       2016-08-01 10:53:19 +08:00
    不要用正则匹配 html 。
    https://pypi.python.org/pypi/beautifulsoup4
    amustart
        8
    amustart  
       2016-08-01 10:55:58 +08:00
    不要用正则匹配 html +1 ,我记得 re 的作者?一直在吐槽这个,你要解析 html 就不能用 beautifulSoup 和其他 HTML 解析器么
    PS : 练手和网页写的渣另算
    lxy
        9
    lxy  
       2016-08-01 10:59:12 +08:00
    @zhongshaohua ^ 表示非,那句就表示匹配所有非双引号的字符。
    imn1
        10
    imn1  
       2016-08-01 11:16:46 +08:00
    @amustart
    PS : 练手和网页写的渣另算
    =======================
    不敢苟同

    你知道加载 DOM 需要耗费多少时间和内存么?试试 parse 百万个 html 和只用 regex 比较一下?
    CharlesL
        11
    CharlesL  
       2016-08-01 11:52:49 +08:00
    http://www.txt2re.com/index.php3
    试试这个正则表达式生成器
    z5864703
        12
    z5864703  
       2016-08-01 16:47:23 +08:00
    @imn1 +10086
    ImPerat0R
        13
    ImPerat0R  
       2016-08-01 22:15:17 +08:00
    slysly759
        14
    slysly759  
       2016-08-05 14:05:29 +08:00
    # 我个人建议如下:
    1. 不到万不得已不用正则表达式
    2. 正则表达式对正文搜索支持和网页数据提取非常不友好
    3. 网页建议使用 xpath ,美丽鸡汤我个人觉得不好用。
    4. xpath 在 Google 有 xpath helper 非常好用,获取哪里点哪里,妈妈再也不用担心我的数据抓取了
    5. 第四条的插件目前只能在 51.xGoogle chrome 以下使用。
    6. 如果非要用正则表达式,那么有一个坑爸爸的正则表达式测试器供你使用。为什么坑爸爸,因为我经常自己写的在测试器可以,但是 python 私活不认,这不,刚买了新电脑。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5389 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 09:08 · PVG 17:08 · LAX 02:08 · JFK 05:08
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.