[英]Matching greater than HTML character with Regex in Python
我正在嘗試使用re.compile來匹配網頁上的值
我的網頁包含以下HTML:
<div id="paginate">
<strong>1</strong>
<a href="http://www.link2.com/">2</a>
<a href="http://www.link3.com/">3</a>
<a href="http://www.link2.com">></a>
<a href="http://www.link20.com/">Last ›</a>
</div>
我的正則表達式如下:
re.compile('<a href="(.+?)">></a>').findall()
這返回
['http://www.link2.com/">2</a>
<a href="http://www.link3.com">3</a>
<a href="http://www.link2.com/']
我只想獲取包含大於符號作為其標簽的鏈接的href?
有任何想法嗎?
提前致謝
只需使用re.findall()
:
>>> re.findall('<a href="(.+?)">></a>', html)
['http://www.link4.com']
請注意,您實際上應該使用HTML解析器而不是regex解析HTML。 我建議BeautifulSoup
:
>>> from bs4 import BeautifulSoup as BS
>>> soup = BS(html)
>>> print soup.find('a', text='>')
<a href="http://www.link4.com">></a>
>>> print soup.find('a', text='>')['href']
http://www.link4.com
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.