推荐学习书目
› 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 · Jul 22, 2016 · 3798 views
    This topic created in 3757 days ago, the information mentioned may be changed or developed.

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

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

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

    用的是 html.parser, 貌似有 bug.
    chairuosen
    chairuosen
    Jul 22, 2016
    9
    就不应该用正则,试试 pyquery
    protream
    protream
    OP
    Jul 22, 2016
    10
    @9hills
    @protream
    值得一试.
    zhouxuchen
    zhouxuchen
    Jul 22, 2016 via iPhone
    11
    pyquery+1
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   4487 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 38ms · UTC 05:43 · PVG 13:43 · LAX 21:43 · JFK 00:43
    ♥ Do have faith in what you're doing.