Python:今日份小收获
爬微博数据,老是有问题 然后看了一下,发现是因为多了一个
这个小版块,然后疯狂搜索,尝试了try:except:还是不行
后来在b站看到了个视频,完美解决 哈哈哈哈啊哈哈
原来的代码
def prase_data(res_json):
if res_json.get('data'):
for node in res_json['data']['cards']:
mblog = node.get('mblog')
if mblog:
item = dict()
item['text'] = PyQuery(node['mblog']['text']).text()
item['created_at'] = node['mblog']['created_at']
item['id'] = node['mblog']['id']
item['screen_name'] = node['mblog']['user']['screen_name']
item['attitudes_count'] = node['mblog']['attitudes_count']
item['comments'] = node['mblog']['comments_count']
item['reposts_count'] = node['mblog']['reposts_count']
print(item)
改过后的代码
def prase_data(res_json):
if res_json.get('data'):
for node in res_json['data']['cards']:
mblog = node.get('mblog')
if mblog:
item = dict()
item['text'] = PyQuery(node['mblog']['text']).text()
item['created_at'] = node['mblog']['created_at']
item['id'] = node['mblog']['id']
item['screen_name'] = node['mblog']['user']['screen_name']
item['attitudes_count'] = node['mblog']['attitudes_count']
item['comments'] = node['mblog']['comments_count']
item['reposts_count'] = node['mblog']['reposts_count']
print(item)