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

你可能感兴趣的文章
Nginx 反向代理解决跨域问题
查看>>
Nginx 反向代理配置去除前缀
查看>>
nginx 后端获取真实ip
查看>>
Nginx 多端口配置和访问异常问题的排查与优化
查看>>
Nginx 如何代理转发传递真实 ip 地址?
查看>>
Nginx 学习总结(16)—— 动静分离、压缩、缓存、黑白名单、性能等内容温习
查看>>
Nginx 学习总结(17)—— 8 个免费开源 Nginx 管理系统,轻松管理 Nginx 站点配置
查看>>
Nginx 学习(一):Nginx 下载和启动
查看>>
nginx 常用指令配置总结
查看>>
Nginx 常用配置清单
查看>>
nginx 常用配置记录
查看>>
nginx 开启ssl模块 [emerg] the “ssl“ parameter requires ngx_http_ssl_module in /usr/local/nginx
查看>>
Nginx 我们必须知道的那些事
查看>>
Nginx 的 proxy_pass 使用简介
查看>>
Nginx 的配置文件中的 keepalive 介绍
查看>>
Nginx 结合 consul 实现动态负载均衡
查看>>
Nginx 负载均衡与权重配置解析
查看>>
Nginx 负载均衡详解
查看>>
nginx 配置 单页面应用的解决方案
查看>>
nginx 配置https(一)—— 自签名证书
查看>>