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

原本我查找所有的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教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

全部回覆(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)


熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!