爬取cn板块前50个帖子内容并制作词云[python]

in #cn8 years ago (edited)

68493.png

_______________________20180826更新线______________________________

新增了词云以logo的形式呈现的代码

# -*- coding: utf-8 -*-
"""
Created on Thu Aug 23 19:51:36 2018

@author: Administrator
"""


from requests_html import HTMLSession
import matplotlib.pyplot as plt
from wordcloud import WordCloud
import jieba
from os import path
import os
import numpy as np
from PIL import Image

session = HTMLSession()
r = session.get('https://steemit.com/trending/cn')

#获取cn首页最热的50个帖子
link = r.html.absolute_links
lis = []
for i in link :
    i = str(i)
    if '@' in i:
        lis.append(i)

#解析每个帖子的内容
def page(url):
    pagecontent = ''
    r= session.get(url)
    r = r.html.find('p',first=False)
    for i in r:
        i = str(i.text)
        pagecontent = pagecontent + i
    return pagecontent


content = ''
for i in lis:
    print(page(i))
    content = content+ page(i)


text = jieba.cut(content,cut_all=True)
text = ' '.join(text)

d = path.dirname(__file__) if "__file__" in locals() else os.getcwd()
steem = np.array(Image.open(path.join(d, "steemit.png")))
wordc = WordCloud(width= 2000,height= 1200,background_color="white", max_words=3000,mask=steem).generate(text)


plt.imshow(wordc)
plt.axis('off')
plt.show()
wordc.to_file('20180826.png')

    
    





_______________________20180823更新线______________________________

没错,是我,猪猪小仙女,我又来了,今天写点什么呢。

按照前两天的尿性,我应该是继续更新我的文本分类进度的
前情可见: 使用支持向量机对文本进行分类 To classify text content by svm[达观杯2]
https://steemit.com/cn/@littlexiannv/to-classify-text-content-by-logistic-svm-2

然而今天新试的两个模型结果都比原来的还要差
有一个SVC的模型,准确率更是跌到了0.5

146125.jpg

本着眼不见为净的心态,决定今晚搞点别的事玩玩,正好最近朋友和我推荐k神之前新写的 requests-html库,就决定试用这个库搞点事,用requests-html库取steemit上cn板块下的帖子,再用jieba分个词,最后画个词云,岂不是美滋滋。

老样子,上代码

from requests_html import HTMLSession
import matplotlib.pyplot as plt
from wordcloud import WordCloud
import jieba

session = HTMLSession()
r = session.get('https://steemit.com/trending/cn')

#获取cn首页最热的50个帖子的url
link = r.html.absolute_links
lis = []
for i in link :
    i = str(i)
    if '@' in i:
        lis.append(i)

#解析每个帖子的内容
def page(url):
    pagecontent = ''
    r= session.get(url)
    r = r.html.find('p',first=False)
    for i in r:
        i = str(i.text)
        pagecontent = pagecontent + i
    return pagecontent


content = ''
for i in lis:
    #print(page(i))
    content = content+ page(i)


text = jieba.cut(content,cut_all=True)
text = ' '.join(text)

wordc = WordCloud(width= 2000,height= 1000,background_color="white", max_words=3000).generate(text)

plt.imshow(wordc)
plt.axis('off')
plt.show()
wordc.to_file('test.png')

最后的效果是这样的

1222222.png
(图有点大,可能要加载一会才能看见)

这样的!

test.png

这个代码还有很多问题,之后要慢慢改,有兴趣一起搞的亲可以在评论里和我讨论哈哈哈

主要要修改的地方:

  1. 目前不支持翻页,只能爬取页面第一次加载的50个帖子,requests-html库本身是支持js翻页的,但是我的电脑一跑html.render() 就会意外意外中断,这个问题只好之后慢慢解决啦
    108643.jpg

  2. 爬取50个帖子的内容都是单线程做的,导致时间比较久,等我去学习一下爬虫怎么写多线程以后来改进这个问题。

  3. 词云停用词的问题,大家可以看到在效果图里其实有很多没啥特别大意义的词, 比如post 啊,using啊这种的,这几个词我已经加到wordcloud的stopword配置里了,但是还是没起效,不知道为啥,这个之后再研究一下哈。

最后总结一下:

  1. k神的新库非常好用,推荐大家都去试一试,地址在这里 https://github.com/kennethreitz/requests-html

  2. 写了这个程序以后,我就可以每天发一次词云和统计了,不愁没内容水贴了,这样一想,离我成为steem大V,日赚斗金,走上人生巅峰的目标又近了一步。

68761.jpg

喜欢我帖子的亲亲可以帮我点个赞,祝我早日走上steem大V,日赚斗金,走上人生巅峰的道路,谢谢!

Sort:  

支持一下!关键词里面那么大的大腿看到没?

哪个大腿?!partiko吗?!我这就去下!

posted using partiko 就别抓了,每个用partiko发的帖子都会有这几个字。

加到停用词了,但是没起效,我之后再改改

這是。。。!!!
驚天地的諭言「Steem 可以 using Partiko」

Posted using Partiko Android

你把我吓死了!

Coin Marketplace

STEEM 0.05
TRX 0.32
JST 0.082
BTC 64892.59
ETH 1765.07
USDT 1.00
SBD 0.42