python3爬虫,遇到某个页面div的class属性缺失,该怎样跳过这一页进行下一页呢?

原本我查找所有的div.class.profile-head信息,但有个页面没有了这一class属性,然后就报错了。我该如何跳过,然后爬取下一页呢?
我的代码如下:

def get_detail():
    detail_links=detail_list()
    names=[]
    for detail in detail_links:
        r=requests.get(detail)
        soup=BeautifulSoup(r.text,'lxml')
        content=soup.find('div',{'class':'profile-head'})
        for name in content.find_all('h2'):
            names.append(name)
阅读 6.1k
1 个回答
def get_detail():
    detail_links=detail_list()
    names=[]
    for detail in detail_links:
        r=requests.get(detail)
        soup=BeautifulSoup(r.text,'lxml')
        content=soup.find('div',{'class':'profile-head'})
        if content is None:
            continue
        for name in content.find_all('h2'):
            names.append(name)
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题