Python 从';a';具有纯文本属性的元素
我试图在python webscraper中构建一个函数,该函数将在结果列表中移动到下一页。我很难在Beauty soup中找到元素,因为该链接位于许多其他标记的末尾,并且没有任何属性,例如class或ID 以下是html的一个片段:Python 从';a';具有纯文本属性的元素,python,beautifulsoup,Python,Beautifulsoup,我试图在python webscraper中构建一个函数,该函数将在结果列表中移动到下一页。我很难在Beauty soup中找到元素,因为该链接位于许多其他标记的末尾,并且没有任何属性,例如class或ID 以下是html的一个片段: <a href="http://www.url?&=page=2"> Next </a> 我一直在阅读bs4文档,试图理解如何提取URL,但我还是被难住了。我认为这可以通过以下方式
<a href="http://www.url?&=page=2">
Next
</a>
我一直在阅读bs4文档,试图理解如何提取URL,但我还是被难住了。我认为这可以通过以下方式实现:
'''
soup=BeautifulSoup(txt,'html.parser')
打印(soup.select('div#block>a')[-1]['href'])
谢谢,这就解决了问题!特别是打印(soup.select_one('a:contains(“Next”))['href'])
from bs4 import BeautifulSoup
txt = '''
<a href="http://www.url?&=page=2">
Next
</a>'''
soup = BeautifulSoup(txt, 'html.parser')
print(soup.select_one('a:contains("Next")')['href'])
http://www.url?&=page=2
print(soup.find('a', text=lambda t: t.strip() == 'Next')['href'])
from bs4 import BeautifulSoup
txt = '''
<div id="block">
<a href="#">Some other link</a>
<a href="http://www.url?&=page=2">Next</a>
</div>
'''
soup = BeautifulSoup(txt, 'html.parser')
print(soup.select('div#block > a')[-1]['href'])