weixin_45947411 2019-12-31 15:41
浏览 856

用BS爬取网页内容之后提取标签属性,显示AttributeError: 'NoneType' object has no attribute 'text'。用print可以成功提取出文本内容,放在循环里就出错。

用BS爬取网页内容之后标签属性一直出错,显示AttributeError: 'NoneType' object has no attribute 'text'

我用print在循环之前试过是可以成功提取出文本内容的,不知道为什么在循环里就不行。求大神解惑!

#s = content[0].find('h5',class_="result-sub-header")
#print(s.text.strip())

#遍历content,取出结果
#因为find_all返回的是一个list,再对list用find_all时,需要指定元素[0]
for i in range(len(content)): 
    #提取标题
    t = content[i].find('a',class_="title")
    title = t.text.strip()
    #提取链接
    url = 'https://www.forrester.com'+t['href']
    #提取摘要
    s = content[i].find('h5',class_="result-sub-header")
    summary = s.text.strip()

    #将提取的内容放在列表paper中
    paper = [title,'Cloud Migration',url,summary]
    #把每个paper加到paperlist
    paperlist.append(paper) 
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 ads仿真结果在圆图上是怎么读数的
    • ¥20 Cotex M3的调试和程序执行方式是什么样的?
    • ¥20 java项目连接sqlserver时报ssl相关错误
    • ¥15 一道python难题3
    • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
    • ¥15 牛顿斯科特系数表表示
    • ¥15 arduino 步进电机
    • ¥20 程序进入HardFault_Handler
    • ¥15 oracle集群安装出bug
    • ¥15 关于#python#的问题:自动化测试