数学建模已结束,刚开始的目标就是不熬夜,结果还是熬夜了(QAQ),缓了一天就来写简书了,感觉很久没爬虫了,今天就爬下移动端的微博好友圈信息。

代码

import requests

import json

headers = {

'Cookie':'xxxxxxxx',

'User_Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36'

}

f = open('C:/Users/LP/Desktop/weibo.txt','a+',encoding='utf-8')

def get_info(url,page):

html = requests.get(url,headers=headers)

json_data = json.loads(html.text)

card_groups = json_data[0]['card_group']

for card_group in card_groups:

f.write(card_group['mblog']['text'].split(' ')[0]+'\n')

next_cursor = json_data[0]['next_cursor']

if page<50:

next_url = 'https://m.weibo.cn/index/friends?format=cards&next_cursor='+str(next_cursor)+'&page=1'

page = page + 1

get_info(next_url,page)

else:

pass

f.close()

if __name__ == '__main__':

url = 'https://m.weibo.cn/index/friends?format=cards'

get_info(url,1)

代码分析

提交cookie模拟登陆微博

好友圈的信息也是异步加载,如图为第一页的url

d8e340f0a247?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

看下返回的数据中,这个next_cursor很重要!!!!

d8e340f0a247?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

往下翻页,如图,可以看出第二页的url中也有next_cursor,刚好是第一页返回的!!!!!

d8e340f0a247?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

这样就可以构造多页url,爬取数据啦

词云

最后,jieba分词做了一个词云,除了二哈,感觉全是群主的,被刷屏了。

d8e340f0a247?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

Logo

GitCode 天启AI是一款由 GitCode 团队打造的智能助手,基于先进的LLM(大语言模型)与多智能体 Agent 技术构建,致力于为用户提供高效、智能、多模态的创作与开发支持。它不仅支持自然语言对话,还具备处理文件、生成 PPT、撰写分析报告、开发 Web 应用等多项能力,真正做到“一句话,让 Al帮你完成复杂任务”。

更多推荐