写给我无聊看的,python爬取CSDN博客标题和摘要出现的最多字,我都不知道我想干什么

news/2024/7/19 11:37:11 标签: 新星计划, python, 爬虫, 词云, 数据可视化

一、分析网页

这个网站是动态加载的数据,我们不墨迹,直接抓包

在这里插入图片描述

  • https://blog.csdn.net/community/home-api/v1/get-business-list?page=1&size=20&businessType=lately&noMore=false&username=Yxh666
  • https://blog.csdn.net/community/home-api/v1/get-business-list?page=2&size=20&businessType=lately&noMore=false&username=Yxh666
  • https://blog.csdn.net/community/home-api/v1/get-business-list?page=3&size=20&businessType=lately&noMore=false&username=Yxh666

注意到这个URL地址是只有 page 是变化的,那我们直接更换页数就可抓取内容

二、技术点

  • 用到的技术
            requests、jieba、wordcloud、matplotlib、numpy、PIL

  • 学习侧重点
            对于jieba和wordcloud模块的使用,以及对动态加载网页获取数据

三、代码编写

python">import requests
import time
import random


class CsdnSpider:
    def __init__(self):
        self.url = 'https://blog.csdn.net/community/home-api/v1/get-business-list?page={}&size=20&businessType=blog&orderby=&noMore=false&username=Yxh666'
        # 写入txt文件
        self.f = open('bolg.txt', 'w', encoding='utf8')

    def get_html(self, url):
        headers = {
            'user-agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.106 Safari/537.36',
        }
        _html = requests.get(url=url, headers=headers).json()
        self.parse_html(_html)

    def parse_html(self, _html):
        result_data = _html['data']['list']

        for res in result_data:
            item = {}
            item['title'] = res['title']
            item['description'] = res['description'].replace("\t", "").replace(' ', '').replace('\xa0', '').replace(
                '\u2003', '').replace('...', '') # 去除一些换行之类的符号
            item['diggCount'] = res['diggCount']
            item['commentCount'] = res['commentCount']
            item['viewCount'] = res['viewCount']
            item['url'] = res['url']
            print(item)
            self.f.write(item['title'])
            self.f.write(item['description'])
            self.f.write("\n")

    def run(self):

        for i in range(1, 6):
            page_url = self.url.format(i)
            self.get_html(url=page_url)
            time.sleep(random.randint(2, 4))
        self.f.close()


if __name__ == '__main__':
    spider = CsdnSpider()
    spider.run()

生成词云代码:
pip install jieba
pip install wordcloud

python">import jieba
from matplotlib import pyplot  as plt
import wordcloud as wc
from PIL import Image
import numpy as np


with open("bolg.txt", "r", encoding="utf-8") as f:
    content = f.read()

res = jieba.lcut(content)
text = " ".join(res)
# 读取图片,用图片展示
mask = np.array(Image.open("logo.jpeg"))

# SimHei.ttf 为字体,要显示中文必须有字体,没有的话去下载一下
word_cloud = wc.WordCloud(font_path="SimHei.ttf", mask=mask)
word_cloud.generate(text)

# 保存云词图片
word_cloud.to_file("blog.png")

plt.imshow(word_cloud)
plt.show()
print(text)

天啊,我这都什么乱七八糟的,快看看们的什么样子啊
在这里插入图片描述
在这里插入图片描述


http://www.niftyadmin.cn/n/835379.html

相关文章

Mybatis持久层配置

Mybatis持久层配置 用mybatis-generator-core-1.3.2.jar插件加jdbc数据库连接包自动导出springmvc持久层dao包,mapper包,model包。 操作准备: 下载:mybatis-generator-core-1.3.2.jar,mysql-connector-java-5.1.34.jar…

找jar包

http://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22javax.servlet%22%20AND%20a%3A%22jstl%22转载于:https://www.cnblogs.com/fpcbk/p/9497127.html

熬夜喝了一瓶冰可乐,突然想到的冒泡排序算法安排

冒泡排序: 通俗解释就是:定义从小到大排列,比较相邻的数字,如果第一个比第二个大,就交换他们两个,如果如果第一个比第二个小,就不动第一个,继续从第二个开始比较。 这个算法的名字…

悄悄地配置C语言环境,代码的不要

一、扯点麻烦事 哈喽哈喽你好,我又是好多天没更新文章了,你可能想,我又放飞自我了,其实这几天真的没有玩,我在煎熬的作斗争 我是一个专科生,但是我心有不甘,我这几天就是在决定到底要不要上本…

java8新特性(三)_Optional类的使用

说实话,我第一次知道这个东西是从阿里规约中,因为公司前一段时间一直在搞代码审核,我的代码写的就感觉很烂,就像规范下。让别人看起来没那么烂。于是就开始看阿里规约,在看到NPE处理的时候,上面提到用Optio…

要悄悄地学C语言,在成为大佬的路上一去不复返

一、什么是C语言 什么是C语言,C语言是一门计算机语言,那什么是计算机语言? 就如世界有好多个国家,每个国家的语言可能都不一样,有汉语、英语、俄罗斯语、法语等等,那么按照正常来说,比如中国人…

python __repr__ __str__

实现一个简单二维向量类 #!/usr/bin/env python # codingutf-8from math import hypotclass Vector:def __init__(self, x0, y0):self.x xself.y y def __repr__(self):"""内置函数repr, 把一个对象用字符串的形式表达出来以便辨认,这就是字符串表示形式.repr…

Coins

题目大意: 给定N个硬币,Ai表示第i个硬币的价值,Ci表示第i个硬币的数量。若从中选出若干个硬币把面值相加,设结果为S,则称“面值S能被拼成”。求1——M之间能拼成的面值有多少个。 解题思路: dp 本题询问的…