Python爬虫 object has no attribute ‘title’ 问题解决
在学习python爬虫时,想要获取title信息,遇到object has no attribute ‘title’ 的问题,仔细看并没有标点和拼写错误
原代码
from urllib.request import urlopen
from bs4 import BeautifulSoup as bf
html=urlopen("http://www.baidu.com/")
obj=bf(html.read() ,'html.parser')
title = obj.head.title
print('---------------------------------------------------')
print(title)
输出如下:
解决
发现此时head为NoneType,尝试输出html.read()的类型:为bytes
而BeautifulSoup的第一个参数为将要解析的文本字符串,因此修改BeautifulSoup函数的第一个参数为