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

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

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

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

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