2017-10-14 178 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'