博客
关于我
微博数据爬虫——获取用户基本信息(三)
阅读量: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 集成Zipkin服务链路追踪
查看>>
nginx 集群配置方式 静态文件处理
查看>>
nginx+php的搭建
查看>>
nginx+tomcat+memcached
查看>>
Nginx+Tomcat实现动静分离
查看>>
nginx+Tomcat性能监控
查看>>
nginx+uwsgi+django
查看>>
nginx+vsftp搭建图片服务器
查看>>
Nginx-http-flv-module流媒体服务器搭建+模拟推流+flv.js在前端html和Vue中播放HTTP-FLV视频流
查看>>
nginx-vts + prometheus 监控nginx
查看>>
nginx: [emerg] getpwnam(“www”) failed 错误处理方法
查看>>
nginx:Error ./configure: error: the HTTP rewrite module requires the PCRE library
查看>>
Nginx、HAProxy、LVS
查看>>
Nginx下配置codeigniter框架方法
查看>>
Nginx中使用expires指令实现配置浏览器缓存
查看>>
Nginx之二:nginx.conf简单配置(参数详解)
查看>>
Nginx代理websocket配置(解决websocket异常断开连接tcp连接不断问题)
查看>>
Nginx代理初探
查看>>