博客
关于我
微博数据爬虫——获取用户基本信息(三)
阅读量:311 次
发布时间:2019-03-01

本文共 1202 字,大约阅读时间需要 4 分钟。

获取微博用户的基本信息可以通过以下步骤实现。目标是获取用户的关注数、粉丝数、微博数量以及注册时间等信息。

获取微博用户的基本信息

第一步:获取 page_id

通过调用微博用户的个人主页 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/

你可能感兴趣的文章
notepad如何自动对齐_notepad++怎么自动排版
查看>>
Notes on Paul Irish's "Things I learned from the jQuery source" casts
查看>>
Notification 使用详解(很全
查看>>
NotImplementedError: Cannot copy out of meta tensor; no data! Please use torch.nn.Module.to_empty()
查看>>
NotImplementedError: Could not run torchvision::nms
查看>>
Now trying to drop the old temporary tablespace, the session hangs.
查看>>
nowcoder—Beauty of Trees
查看>>
np.arange()和np.linspace()绘制logistic回归图像时得到不同的结果?
查看>>
np.power的使用
查看>>
NPM 2FA双重认证的设置方法
查看>>
npm build报错Cannot find module ‘webpack/lib/rules/BasicEffectRulePlugin‘解决方法
查看>>
npm build报错Cannot find module ‘webpack‘解决方法
查看>>
npm ERR! ERESOLVE could not resolve报错
查看>>
npm ERR! fatal: unable to connect to github.com:
查看>>
npm ERR! Unexpected end of JSON input while parsing near '...on":"0.10.3","direc to'
查看>>
npm ERR! Unexpected end of JSON input while parsing near ‘...“:“^1.2.0“,“vue-html-‘ npm ERR! A comp
查看>>
npm error Missing script: “server“npm errornpm error Did you mean this?npm error npm run serve
查看>>
npm error MSB3428: 未能加载 Visual C++ 组件“VCBuild.exe”。要解决此问题,1) 安装
查看>>
npm install CERT_HAS_EXPIRED解决方法
查看>>
npm install digital envelope routines::unsupported解决方法
查看>>