python学习1:Ajax结果提取之微博爬取
@《Python3网络爬虫开发实战》238页
相比,原代码中对第一个ajax请求下的cards字段下存在改变,第二个元素多了一个无关微博内容的关注信息,因此for循环会报错。
问题:for 循环中出现异常时如何跳过继续处理?
解决办法:
try:
weibo={}
weibo['id']=item.get('id')
weibo['text']=pq(item.get('text')).text()
weibo['attitudes']=item.get('attitudes_count')
weibo['comments']=item.get('comments_count')
weibo['reposts']=item.get('reposts_count')
yield weibo
except:
continue
此外,发现微博域名的规律,现在的url结构改变,可从手机端获取链接,然后将后面的数字号码代入https://m.weibo.cn/u/