本文共 1222 字,大约阅读时间需要 4 分钟。
获取微博用户的基本信息可以通过以下步骤实现。目标是获取用户的关注数、粉丝数、微博数量以及注册时间等信息。
通过调用微博用户的个人主页 URL,可以获取到用户的 page_id。page_id 是一个唯一标识符,可以用于后续获取用户详细信息的操作。
import refrom urllib import requestimport configdef get_user_action(o_id): headers = config.get_headers() add = request.Request(url="https://weibo.com/u/%s?is_all=1" % (o_id), headers=headers) response = request.urlopen(add, timeout=20).read().decode('utf-8') page_id = re.findall(r'\$CONFIG\[\'page_id\']=\'(\d+)\'', response)[0]
使用 page_id 调用用户信息页面,通过正则匹配提取关注数、粉丝数、微博数量以及注册时间等信息。
add = request.Request(url="https://weibo.com/p/%s/info" % (page_id), headers=headers)response = request.urlopen(add, timeout=20).read().decode('utf-8')# 提取关注数、粉丝数、微博数量等信息follow_num = re.findall(r'(\d+)', response)[0]fans_num = re.findall(r'(\d+)', response)[1]post_num = re.findall(r'(\d+)', response)[2]# 提取注册时间regist_time = re.findall(r'注册时间:.*?(\d+)', response)[0]regist_time = regist_time.replace(" ", "").replace("\\r\\n", "")
提取到的信息将存储在一个字典中,返回如下结果:
{ 'follow_num': follow_num, 'fans_num': fans_num, 'post_num': post_num, 'regist_time': regist_time}
以上代码结合了正则匹配技术,能够高效地从微博用户页面中提取所需的信息。通过合理的请求参数设置和响应数据处理,可以实现对微博用户基本信息的有效获取。
转载地址:http://joio.baihongyu.com/