>  Q&A  >  본문

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)


高洛峰高洛峰2914일 전1261

모든 응답(1)나는 대답할 것이다

  • 三叔

    三叔2016-10-26 15:48:20

    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)


    회신하다
    0
  • 취소회신하다