Heim  >  Fragen und Antworten  >  Hauptteil

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 Tage vor1266

Antworte allen(1)Ich werde antworten

  • 三叔

    三叔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)


    Antwort
    0
  • StornierenAntwort