爬取知网摘要发生错误

wyx9822 2021-06-11 10:05:43

    下面是用python爬取知网摘要的部分代码,试运行后发生“AttributeError: 'NoneType' object has no attribute 'span'    ”错误,请问各位大佬这该怎么解决呀?

        html2 = driver.page_source
        soup = BeautifulSoup(html2, 'html.parser')
        text = soup.text
        text2 = text[re.search('摘要:', text).span()[1]:re.search('手机阅读本文', text).span()[0]]
        if '分类号:' in text2:
            tmp = text2[:re.search('分类号:', text2).span()[0]]
            keywords = tmp[re.search('关键词:', tmp).span()[1]:].strip().replace('\n',' ').replace(' ','')
        else:
            keywords = ''
        driver.close()
        driver.switch_to.window(driver.window_handles[1])
        driver.switch_to.frame(driver.find_element_by_id('iframeResult')) 
        dictionary = {
                "No.":No,
                "Title":title,
                "Abstract":ab,
                "Keywords":keywords,
                "Organizations":organizations,
                "Authors":authors,
                "Source":source,
                "Time":times,
                # "database":database,
                "Counted":counted,
                "DownloadCount":downloadCount,
                }
        print('这是第',page_mark,'页第',No,'条记录,标题为:',title)
        store_data(dictionary)
        time.sleep(random.uniform(5,8))
...全文
888 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
兰振lanzhen 2021-06-17
  • 打赏
  • 举报
回复

1、爬虫遇到这个,如果是可以获取部分内容,那说明有些页面是空得,re.search 返回得是空值,可以使用try except来过滤;

2、如果是完全都获取不到,那检查下这个re.search是否坑匹配到信息。

37,719

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧