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 年 7 月 22 日 · 3165 次点击
    这是一个创建于 3467 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

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

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

    用的是 html.parser, 貌似有 bug.
    chairuosen
        9
    chairuosen  
       2016 年 7 月 22 日
    就不应该用正则,试试 pyquery
    protream
        10
    protream  
    OP
       2016 年 7 月 22 日
    @9hills
    @protream
    值得一试.
    zhouxuchen
        11
    zhouxuchen  
       2016 年 7 月 22 日 via iPhone
    pyquery+1
    关于   ·   帮助文档   ·   自助推广系统   ·   博客   ·   API   ·   FAQ   ·   Solana   ·   3047 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 22ms · UTC 00:18 · PVG 08:18 · LAX 16:18 · JFK 19:18
    ♥ Do have faith in what you're doing.