微博(APP)榜单爬虫及数据可视化

3629157-26407d1cfc8be1ac.png

前言

今天继续APP爬虫,今天爬取的是微博榜单(24小时榜)的数据,采集的字段有:

  • 用户id
  • 用户地区
  • 用户性别
  • 用户粉丝
  • 微博内容
  • 发布时间
  • 转发、评论和点赞量

该文分以下内容:

  • 爬虫代码
  • 用户分析
  • 微博分析
3629157-d77f37885d7a2671.jpg
3629157-e442410f33b8bfaf.png

爬虫代码

import requests
import json
import re
import time
import csv

headers = {
    'Host': 'api.weibo.cn',
    'Connection': 'keep-alive',
    'User-Agent': 'Weibo/29278 (iPhone; iOS 11.4.1; Scale/2.00)'
}

f = open('1.csv','w+',encoding='utf-8',newline='')
writer = csv.writer(f)
writer.writerow(['user_id','user_location','user_gender','user_follower','text','created_time','reposts_count','comments_count','attitudes_count'])

def get_info(url):
    res = requests.get(url,headers=headers)
    print(url)
    datas = re.findall('"mblog":(.*?),"weibo_position"',res.text,re.S)
    for data in datas:
        json_data = json.loads(data+'}')
        user_id = json_data['user']['name']
        user_location = json_data['user']['location']
        user_gender = json_data['user']['gender']
        user_follower = json_data['user']['followers_count']
        text = json_data['text']
        created_time = json_data['created_at']
        reposts_count = json_data['reposts_count']
        comments_count = json_data['comments_count']
        attitudes_count = json_data['attitudes_count']
        print(user_id,user_location,user_gender,user_follower,text,created_time,reposts_count,comments_count,attitudes_count)
        writer.writerow([user_id,user_location,user_gender,user_follower,text,created_time,reposts_count,comments_count,attitudes_count])
    time.sleep(5)

if __name__ == '__main__':
    urls = ['https://api.weibo.cn/2/cardlist?gsid=_2A252dh7LDeRxGeNM41oV-S_MzDSIHXVTIhUDrDV6PUJbkdANLVTwkWpNSf8_0j6hqTyDS0clYi-pzwDc2Kd8oj_d&wm=3333_2001&i=b9f7194&b=0&from=1088193010&c=iphone&networktype=wifi&v_p=63&skin=default&v_f=1&s=ef8eeeee&lang=zh_CN&sflag=1&ua=iPhone8,1__weibo__8.8.1__iphone__os11.4.1&ft=11&aid=01AuxGxLabPA7Vzz8ZXBUpkeJqWbJ1woycR3lFBdLhoxgQC1I.&moduleID=pagecard&scenes=0&uicode=10000327&luicode=10000010&count=20&extparam=discover&containerid=102803_ctg1_8999_-_ctg1_8999_home&fid=102803_ctg1_8999_-_ctg1_8999_home&lfid=231091&page={}'.format(str(i)) for i in range(1,16)]
    for url in urls:
        get_info(url)

用户分析

首先对部分用户id进行可视化,字体大一点的是上榜2次的(这次统计中最多上榜的是2次)。

3629157-8b3f4afd0e5ab67f.png

接着对地区进行数据处理,进行统计。可以看出,位于北京的用户是最多的(大V都在北京)。

df['location'] = df['user_location'].str.split(' ').str[0]
3629157-a5f2651355f13030.png

接下来看下用户的性别比例:男性用户占多。

3629157-0a11316825ff43ac.png

最后再看看上榜大V粉丝前十:

3629157-23147628c1cd5000.png

微博分析

首先,对时间数据进行处理,取出小时时间段。

3629157-6d94af89d4043404.png

接着,我们看看微博点赞前十的用户。

3629157-014ea8668ed551f2.png

最后,绘制微博文章词云图。

3629157-b0a44fabf4326a67.png
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值