python 字符串匹配问题
ringa_lee
ringa_lee 2017-04-18 10:32:04
0
2
448

想匹配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/(.+?)';"))
        

还是返回空,请教各位怎么写才行,是哪里出了问题

ringa_lee
ringa_lee

ringa_lee

répondre à tous(2)
洪涛

Ce tas de choses devant vous n’ont en fait pas besoin d’être assorties.

Il suffit de faire correspondre ceux-ci :

re.findall(r"location.href='/u012582664/article/details/(\d+)",html)
迷茫

Essayez d'échapper aux parenthèses ? Les parenthèses et les crochets ont une signification particulière dans les expressions régulières

Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!