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
protream
V2EX  ›  Python

正则表达式问题

  •  
  •   protream · 2016-07-22 16:39:17 +08:00 · 2563 次点击
    这是一个创建于 2839 天前的主题,其中的信息可能已经有所发展或是发生改变。

    如何写一个正则表达式匹配这个网页中的歌词:小幸运 我试写了几次没能写正确.

    11 条回复    2016-07-22 18:18:03 +08:00
    knightdf
        1
    knightdf  
       2016-07-22 17:37:44 +08:00
    用 xpath 把。。
    Hello1995
        2
    Hello1995  
       2016-07-22 17:46:18 +08:00 via Android
    找到 api ,直接用?
    yexiaoxing
        3
    yexiaoxing  
       2016-07-22 17:54:02 +08:00
    r'(?:<div class="lrc_main">)((.|\n)*)(?:<\/div>)'

    或者……抓 class lrc_main 的内容嘛
    protream
        4
    protream  
    OP
       2016-07-22 18:07:08 +08:00
    @Hello1995
    没有找到.
    protream
        5
    protream  
    OP
       2016-07-22 18:08:42 +08:00
    @yexiaoxing 这个有问题.
    我试了用 beautifulsoup 抓 lrc_main 的内容, 但是有问题, 你可以试试
    9hills
        6
    9hills  
       2016-07-22 18:09:31 +08:00
    https://regex101.com/ 之类的网站 debug 吧
    yexiaoxing
        7
    yexiaoxing  
       2016-07-22 18:10:57 +08:00
    @protream 问题是啥?
    protream
        8
    protream  
    OP
       2016-07-22 18:15:33 +08:00
    @yexiaoxing
    ```
    lyric = soup.find('div', class_='lrc_main')
    ```
    结果如下:
    <div class="lrc_main">
    让我能做回我自己我勇气<br/>幸运<br/> <br/>在青青草地<br/>
    </div>

    用的是 html.parser, 貌似有 bug.
    chairuosen
        9
    chairuosen  
       2016-07-22 18:16:22 +08:00
    就不应该用正则,试试 pyquery
    protream
        10
    protream  
    OP
       2016-07-22 18:17:51 +08:00
    @9hills
    @protream
    值得一试.
    zhouxuchen
        11
    zhouxuchen  
       2016-07-22 18:18:03 +08:00 via iPhone
    pyquery+1
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5180 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 09:11 · PVG 17:11 · LAX 02:11 · JFK 05:11
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.