有没有办法提取特定的<li>
基于 Python 中包含的单词的 HTML 页面?
例如: 我们以这个页面为例:https://en.wikipedia.org/wiki/1916
我用 Python 得到这个页面的 HTML,如下所示:
opener = urllib2.build_opener()
opener.addheaders = [('User-agent', 'Mozilla/5.0')]
infile = opener.open('https://en.wikipedia.org/wiki/1916')
我想要的是得到每个 <li>
包含给定单词。如果我搜索“verdun”,我希望拥有所有 <li>
他们的内容有这个词,比如
<li><a href="/wiki/February_21" title="February 21">February 21</a> – WWI: The <a href="/wiki/Battle_of_Verdun" title="Battle of Verdun">Battle of Verdun</a> begins in <a href="/wiki/French_Third_Republic" title="French Third Republic">France</a>.</li>
最佳答案
你可以简单地这样做,
soup = BeautifulSoup(html)
print([i for i in soup.select('li') if 'verdun' in i])
关于python - 从 HTML 页面获取带有特定关键字的 <li> - Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30850960/