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. python - 數據與循環次數對應不上2. node.js - nodejs開發中常用的連接mysql的庫3. mysql - jdbc的問題4. python - 我在使用pip install -r requirements.txt下載時,為什么部分能下載,部分不能下載5. 視頻文件不能播放,怎么辦?6. mysql - 分庫分表、分區、讀寫分離 這些都是用在什么場景下 ,會帶來哪些效率或者其他方面的好處7. windows7 ping不通虛擬機VMware上的linux(ubuntu)的ip8. 網頁爬蟲 - python 爬取網站 并解析非json內容9. python - 編碼問題求助10. mysql - 如何減少使用或者不用LEFT JOIN查詢?
