Python AttributeError:'NoneType'对象没有属性'get_text'

时间:2016-05-03 12:20:25

标签: python beautifulsoup urllib

from bs4 import BeautifulSoup

from urllib.request import urlopen

fout = open('words_list2.txt','w')

url = 'http://endic.naver.com/?sLn=kr'

doc = urlopen(url)

web_page = BeautifulSoup(doc, 'html.parser')

word = web_page.find(attrs={'class':"tit"})

definition = web_page.find(attrs={'class':"align_line"})

fout.write(word.get_text()+':'+ definition.get_text().replace('\u200b',''))

fout.close()

1 个答案:

答案 0 :(得分:0)

在网址http://endic.naver.com/?sLn=kr,没有类align_line的元素,因此web_page.find(attrs={'class':"align_line"})会返回None,因此definitionNone和{{ 1}}无法正常工作