栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

NavigableString‘ object has no attribute ‘find

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

NavigableString‘ object has no attribute ‘find

 
url='https://book.douban.com/'
    headers={'user-agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36'}
    data=requests.get(url,headers=headers)
    # print(data.text)
    #解释
    print('Phrase by BeautifulSoup')
    soup=BeautifulSoup(data.text,'lxml')
    print(soup)
    books_left=soup.find('ul',{'class':'list-col list-col5 list-express slide-item'})
    boos_left=books_left.find_all('li')
    books=list(books_left)
    img_urls=[]
    titles=[]
    ratings=[]
    authors=[]
    details=[]
    for book in books:
         if  type(book) is not bs4.NavigableString:
            img_url=book.find_all('a')[0].find('img').get('src')
            title=book.find_all('a')[0].get('title')
            print("image_url:"+img_url)
            print("title:" + title)
一、分析过程


1.代码如上所示:

2.出错位置:img_url=book.find_all('a')[0].find('img').get('src')

3.调试监听分析:books 中有 “n”空行

二、解决方法:

加判断过滤空行:if  type(book) is not bs4.NavigableString:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/701175.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号