正则表达式问题

2016-07-22 16:39:17 +08:00
 protream

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

2577 次点击
所在节点    Python
11 条回复
knightdf
2016-07-22 17:37:44 +08:00
用 xpath 把。。
Hello1995
2016-07-22 17:46:18 +08:00
找到 api ,直接用?
yexiaoxing
2016-07-22 17:54:02 +08:00
r'(?:<div class="lrc_main">)((.|\n)*)(?:<\/div>)'

或者……抓 class lrc_main 的内容嘛
protream
2016-07-22 18:07:08 +08:00
@Hello1995
没有找到.
protream
2016-07-22 18:08:42 +08:00
@yexiaoxing 这个有问题.
我试了用 beautifulsoup 抓 lrc_main 的内容, 但是有问题, 你可以试试
9hills
2016-07-22 18:09:31 +08:00
https://regex101.com/ 之类的网站 debug 吧
yexiaoxing
2016-07-22 18:10:57 +08:00
@protream 问题是啥?
protream
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
2016-07-22 18:16:22 +08:00
就不应该用正则,试试 pyquery
protream
2016-07-22 18:17:51 +08:00
@9hills
@protream
值得一试.
zhouxuchen
2016-07-22 18:18:03 +08:00
pyquery+1

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/294243

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX