Python 3 BeautifulSoup打印错误

时间:2015-05-12 20:09:59

标签: python beautifulsoup

嗨,这是我的功能:

def dictionary_search():
    from bs4 import BeautifulSoup
    from urllib.request import urlopen
    url = "http://dictionary.reference.com/browse/"+search_box.getText()+"?s=t"
    page = urlopen(url)
    soup = BeautifulSoup(page.read())
    defini = soup.find("div",{"class":"def-content"}).contents
    print(defini)

这是大型搜索引擎的一部分,我的问题是当我使用print(defini)时。例如,如果我的搜索是" dog"结果将是:

['\na domesticated canid, ', <span class="dbox-italic">Canis familiaris,</span>, ' bred in many varieties. ']

我想删除多余的信息,然后获得定义。

1 个答案:

答案 0 :(得分:0)

def dictionary_search():
    from bs4 import BeautifulSoup
    from urllib.request import urlopen
    url = "http://dictionary.reference.com/browse/"+search_box.getText()+"?s=t"
    page = urlopen(url)
    soup = BeautifulSoup(page.read())
    defini = soup.find("div",{"class":"def-content"}).text
    print(defini)

内容正在采取包括标签在内的所有内容。