37,719
社区成员
发帖
与我相关
我的任务
分享
下面是用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))
1、爬虫遇到这个,如果是可以获取部分内容,那说明有些页面是空得,re.search 返回得是空值,可以使用try except来过滤;
2、如果是完全都获取不到,那检查下这个re.search是否坑匹配到信息。