python 字符串匹配问题
问题描述
想匹配html = <p class='back fl'><a href='javascript:void(0);' onclick='_gaq.push([’_trackEvent’,’function’, ’onclick’, ’blog_articles_shangyipian’]);location.href=’/u012582664/article/details/56845037’;'><span><i class='fa fa-arrow-left'></i></span><em>安装最新版python</em></a></p><p class='forward fr'><a href='javascript:void(0);' onclick='_gaq.push([’_trackEvent’,’function’, ’onclick’, ’blog_articles_xiayipian’]);location.href=’/u012582664/article/details/59120585’;'><em>各种数据库的注释</em><span><i class='fa fa-arrow-right'></i></span></a></p>中的‘56845037’和‘59120585’,尝试用正则:
pattern_l = r’’’<a href='javascript:void(0);' onclick='_gaq.push([’_trackEvent’,’function’, ’onclick’, ’blog_articles_shangyipian’]);location.href=’(.+?)’;'>’’’re.findall(pattern_l,html)
结果不成功。返回为空,有用:
soup = BeautifulSoup(html, 'lxml')print(soup.find_all(onclick='_gaq.push([’_trackEvent’,’function’, ’onclick’, ’blog_articles_shangyipian’]);location.href=’/u012582664/article/details/(.+?)’;'))
还是返回空,请教各位怎么写才行,是哪里出了问题
问题解答
回答1:前面那一大堆东西其实用不着匹配。
匹配这些就够了:
re.findall(r'location.href=’/u012582664/article/details/(d+)',html)回答2:
括号转义下试试?小括号和中括号在正则里有特殊意义
相关文章:
1. angular.js使用$resource服务把数据存入mongodb的问题。2. docker - 各位电脑上有多少个容器啊?容器一多,自己都搞混了,咋办呢?3. vim - docker中新的ubuntu12.04镜像,运行vi提示,找不到命名.4. docker不显示端口映射呢?5. docker容器呢SSH为什么连不通呢?6. android - 优酷的安卓及苹果app还在使用flash技术吗?7. docker start -a dockername 老是卡住,什么情况?8. dockerfile - [docker build image失败- npm install]9. docker-compose中volumes的问题10. debian - docker依赖的aufs-tools源码哪里可以找到啊?

网公网安备