博客
关于我
微博数据爬虫——获取用户基本信息(三)
阅读量: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/

你可能感兴趣的文章
NN&DL4.1 Deep L-layer neural network简介
查看>>
NN&DL4.3 Getting your matrix dimensions right
查看>>
NN&DL4.7 Parameters vs Hyperparameters
查看>>
NN&DL4.8 What does this have to do with the brain?
查看>>
nnU-Net 终极指南
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
NO 157 去掉禅道访问地址中的zentao
查看>>
no available service ‘default‘ found, please make sure registry config corre seata
查看>>
No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
查看>>
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>
No module named 'pandads'
查看>>
No module named cv2
查看>>
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>