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

简介: 前言今天继续APP爬虫,今天爬取的是微博榜单(24小时榜)的数据,采集的字段有:用户id用户地区用户性别用户粉丝微博内容发布时间转发、评论和点赞量该文分以下内容:爬虫代码用户分析微博分析...
img_093660b0ac25e4cc32655308e9b5fff9.png

前言

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

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

该文分以下内容:

  • 爬虫代码
  • 用户分析
  • 微博分析
img_fdb60917401280e7c26bbafb46e51f07.jpe
img_a41f95e3470d427c8fc290b9165d8cb3.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次)。

img_1fcf8f8c8d6157446ad1671a0fdb9038.png

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

df['location'] = df['user_location'].str.split(' ').str[0]
img_355aded7f6fe30cd0faa6e10108dd1cc.png

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

img_ff30c798c66f0ef70d337a9c55a0db8e.png

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

img_acaf5dae0467852e8dc6c758259c06e2.png

微博分析

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

img_863b16a9b6401d9718d26f5a9bd9d12a.png

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

img_eee894092a6920eae2d2b9440dab229c.png

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

img_827558bda74dd2986ec0b1c083803c1e.png
相关文章
|
2天前
|
数据采集 Web App开发 JavaScript
JavaScript爬虫进阶攻略:从网页采集到数据可视化
JavaScript爬虫进阶攻略:从网页采集到数据可视化
|
2天前
|
数据采集 JSON 数据格式
python爬虫之app爬取-charles的使用
charles 基本原理,charles抓包,分析,重发。
77 0
|
2天前
|
数据采集 人工智能 数据可视化
Scala多线程爬虫程序的数据可视化与分析实践
Scala多线程爬虫程序的数据可视化与分析实践
|
2天前
|
数据采集 测试技术 API
python爬虫之app爬取-微信朋友圈
搭建appium环境,appium基本使用,API操作等等
100 0
|
2天前
|
数据采集 Web App开发 数据可视化
Python爬虫技术与数据可视化:Numpy、pandas、Matplotlib的黄金组合
Python爬虫技术与数据可视化:Numpy、pandas、Matplotlib的黄金组合
|
2天前
|
数据采集 存储 安全
python爬虫之app爬取-mitmproxy 的使用
mitmproxy抓包原理,设置代理,MitmDump运用,mitmproxy使用。
51 0
|
数据采集 JSON 网络安全
Python爬虫:爬取手机App数据,记得安装配置Charles
Python爬虫:爬取手机App数据,记得安装配置Charles
473 0
|
数据采集 编解码 数据可视化
干货 | Python爬虫实战(中):数据可视化-教你做出漂亮的图表
干货 | Python爬虫实战(中):数据可视化-教你做出漂亮的图表
526 0
干货 | Python爬虫实战(中):数据可视化-教你做出漂亮的图表
|
数据采集 Python
网站与APP抓包分析3 通过Python实现APP内容爬虫
Python,APP爬虫,题库,Fidder工具
1703 0
网站与APP抓包分析3 通过Python实现APP内容爬虫
|
数据采集 搜索推荐
爬虫---如何抓取app的思路和方案
爬虫---如何抓取app的思路和方案背景2015年,谷歌开始对外部App的内部链接和内容进行抓取,目前已经累计抓取了300多亿个。 搜索引擎是内容门户之后的互联网第二次重大技术革命。然而伴随着智能手机的普及,应用软件(APP)取代网页,成为主流的技术。
1832 0