python3爬虫,遇到某个页面div的class属性缺失,该怎样跳过这一页进行下一页呢?
高洛峰
高洛峰 2016-10-26 15:47:47
0
1
1262

原本我查找所有的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)


高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

répondre à tous(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)


Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!