2017-10-14 137 views

回答

0

试试这个。

from bs4 import BeautifulSoup 

html=''' 
<li class="title"><h4><a href="/addons/wow/world-quest-tracker">World Quest Tracker</a></h4></li> 
''' 
soup = BeautifulSoup(html, "lxml") 
for item in soup.select(".title"): 
    print(item.text) 

结果:

World Quest Tracker 
0
html_doc = '<li class="title"><h4><a href="/addons/wow/world-quest-tracker">World Quest Tracker</a></h4></li>' 
soup = BeautifulSoup(html_doc, 'html.parser') 
print soup.find('a').text 

这将打印

u'World任务追踪”

0

我试图提取文本其间将href标签

如果你确实想在href属性的文字,而不是文本内容由<a></a>锚定(您的措辞有点不清楚),请使用get('href')

from bs4 import BeautifulSoup 

html = '<li class="title"><h4><a href="/addons/wow/world-quest-tracker">World Quest Tracker</a></h4></li>' 
soup = BeautifulSoup(html, 'lxml') 
soup.find('a').get('href') 

'/addons/wow/world-quest-tracker'