import re
from bs4 import BeautifulSoup
soup = BeautifulSoup(html, 'lxml')
for link in soup.find_all('a', string=re.compile(r'^text link')):
print link
from bs4 import BeautifulSoup
html = """<h1 class="titleClass" itemprop="name">
Text title here
<a class="titleLink" href="somelink-here.html">
text link here
</a>
</h1>"""
soup = BeautifulSoup(html)
p = soup.find('h1', attrs={'class': 'titleClass'})
p.a.extract()
print p.text.strip()
您可以使用regex和beauthoulsoup的组合:
这将查找以
text link
开头的所有链接。在导航到
<h1>
,并从.stripped_strings
生成器获取第一个字符串:您可以获取整个
h1
标记,然后提取任何链接,如下所示:这将显示:
^{pr2}$相关问题 更多 >
编程相关推荐