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

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

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

获取微博用户的基本信息

第一步:获取 page_id

通过调用微博用户的个人主页 URL,可以获取到用户的 page_id。page_id 是一个唯一标识符,可以用于后续获取用户详细信息的操作。

import re
from urllib import request
import config
def 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/

你可能感兴趣的文章
No resource identifier found for attribute 'srcCompat' in package的解决办法
查看>>
no session found for current thread
查看>>
No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
查看>>
NO.23 ZenTaoPHP目录结构
查看>>
no1
查看>>
NO32 网络层次及OSI7层模型--TCP三次握手四次断开--子网划分
查看>>
NOAA(美国海洋和大气管理局)气象数据获取与POI点数据获取
查看>>
NoClassDefFoundError: org/springframework/boot/context/properties/ConfigurationBeanFactoryMetadata
查看>>
node exporter完整版
查看>>
Node JS: < 一> 初识Node JS
查看>>
Node Sass does not yet support your current environment: Windows 64-bit with Unsupported runtime(72)
查看>>
Node 裁切图片的方法
查看>>
Node+Express连接mysql实现增删改查
查看>>
node, nvm, npm,pnpm,以前简单的前端环境为什么越来越复杂
查看>>
Node-RED中Button按钮组件和TextInput文字输入组件的使用
查看>>
Node-RED中Switch开关和Dropdown选择组件的使用
查看>>
Node-RED中使用html节点爬取HTML网页资料之爬取Node-RED的最新版本
查看>>
Node-RED中使用JSON数据建立web网站
查看>>
Node-RED中使用json节点解析JSON数据
查看>>
Node-RED中使用node-random节点来实现随机数在折线图中显示
查看>>