import urllib2
from bs4 import BeautifulSoup
url = 'http://baike.baidu.com/view/21087.htm'
response = urllib2.urlopen(url)
if response.getcode() != 200:
print 'error'
html_cont = response.read()
soup = BeautifulSoup(html_cont, 'html.parser', from_encoding="utf-8")
# <dd class="lemmaWgt-lemmaTitle-title"><h1>.com</h1>
#print html_cont
title_node = soup.find('dd', class_="lemmaWgt-lemmaTitle-title")
print title_node.find('h1').get_text()下面是报错Traceback (most recent call last):
File "/home/kinredon/PycharmProjects/imooc2.7/test/testbaike.py", line 16, in <module>
print title_node.find('h1').get_text()
AttributeError: 'NoneType' object has no attribute 'find'经过多次测试,问题就出在soup.find()那,这个该怎么解决啊
1 回答
已采纳
空34
TA贡献1条经验 获得超0个赞
title_node = soup.find('dd', class_="lemmaWgt-lemmaTitle-title")
title_node 可能需要定义为字典,或者是列表
并且不能直接通过get_next()获取,要经过遍历获取
添加回答
举报
0/150
提交
取消