1. 需要的三个包:

from wordcloud import WordCloud        #词云库
import matplotlib.pyplot as plt #数学绘图库
import jieba;

2. 定义变量(将对于的变量到一个全局的文件中):

import re;
pdurl_first='https://movie.douban.com/subject/26363254/comments?start=0'
head={'User-Agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/59.0.3071.109 Chrome/59.0.3071.109 Safari/537.36'}
reg=re.compile(r'<a href="(.*?)&amp;.*?class="next">') #下一页
cookies={"__utma":"30149280.503249607.1504402391.1504402391.1504402391.1",
"_utmb":"30149280.2.9.1504402391","__utmc":"","__utmt":"",
"__utmz":"30149280.1504402391.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)",
"ap":"","as":'"https://movie.douban.com/subject/26363254/comments?start=225&limit=20&sort=new_score&status=P"',
"bid":"g7k4BGd2sRk","ck":"76vs","dbcl2":'"166279730:fohmXhoM9uU"',"ps":"y","push_doumail_num":"",
"push_doumail_num":""}

3. 抓取数据

import requests;
import re;
from GrabData import Param;
import pandas as pd;
from bs4 import BeautifulSoup; class GrabComent:
ren = re.compile(r'<span class="votes">(.*?)</span>.*?comment">.*?</span>.*?<span.*?class="">(.*?)</a>.*?<span>(.*?)</span>.*?title="(.*?)"></span>.*?title="(.*?)"><p .*? > (.*?)</p>',re.S)
def __init__(self):
print('开始抓取数据');
html = requests.get(Param.pdurl_first, headers=Param.head, cookies=Param.cookies);
while html.status_code == 200:
url_next = 'https://movie.douban.com/subject/26363254/comments' + re.findall(Param.reg, html.text)[0]
zhanlang = re.findall(self.ren, html.text)
print(zhanlang)
data = pd.DataFrame(zhanlang)
data.to_csv('H:\\python_projects\\ticket\\zhanlangpinglun.csv', header=False, index=False,
mode='a+') # 写入csv文件,'a+'是追加模式
data = []
zhanlang = []
print("下一页地址:"+url_next);
html = requests.get(url_next, cookies=Param.cookies, headers=Param.head) if __name__ == '__main__':
GrabComent();

4. 生成云图

from wordcloud import WordCloud        #词云库
import matplotlib.pyplot as plt #数学绘图库
import jieba; class WordYun: def __init__(self):
print("开始读取文件!");
self.main(); def main(self):
text = self.readFile();
self.showTitle(text); def showTitle(self,text1):
wc1 = WordCloud(
background_color="white",
width=1000,
height=860,
font_path="D:\\Windows\\Fonts\\STFANGSO.ttf", # 不加这一句显示口字形乱码
margin=2); wc2 = wc1.generate(text1) # 我们观察到generate()接受一个Unicode的对象,所以之前要把文本处理成unicode类型
plt.imshow(wc2)
plt.axis("off")
plt.show(); def readFile(self):
a = []
f = open(r'H:\\python_projects\\ticket\\zhanlangpinglun.csv', 'r').read()
words = list(jieba.cut(f))
for word in words:
if len(word) > 1:
a.append(word);
txt = r' '.join(a)
print("readFile返回的结果:"+txt);
return txt; if __name__ == '__main__':
WordYun();

python 网页爬取数据生成文字云图的更多相关文章

  1. Python分页爬取数据的分析

    前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者: 向右奔跑 PS:如有需要Python学习资料的小伙伴可以加点击下方链 ...

  2. Python爬虫爬取数据的步骤

    爬虫: 网络爬虫是捜索引擎抓取系统(Baidu.Google等)的重要组成部分.主要目的是将互联网上的网页下载到本地,形成一个互联网内容的镜像备份. 步骤: 第一步:获取网页链接 1.观察需要爬取的多 ...

  3. 菜鸟学IT之python网页爬取多页爬取

    作业来源:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE1/homework/3002 0.从新闻url获取点击次数,并整理成函数 newsUrl news ...

  4. 菜鸟学IT之python网页爬取初体验

    作业来源:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE1/homework/2881 1. 简单说明爬虫原理 爬虫简单来说就是通过程序模拟浏览器放松请求站 ...

  5. python requests 爬取数据

    import requests from lxml import etree import time import pymysql import json headers={ 'User-Agent' ...

  6. python爬虫—爬取百度百科数据

    爬虫框架:开发平台 centos6.7 根据慕课网爬虫教程编写代码 片区百度百科url,标题,内容 分为4个模块:html_downloader.py 下载器 html_outputer.py 爬取数 ...

  7. python爬虫——爬取网页数据和解析数据

    1.网络爬虫的基本概念 网络爬虫(又称网络蜘蛛,机器人),就是模拟客户端发送网络请求,接收请求响应,一种按照一定的规则,自动地抓取互联网信息的程序.只要浏览器能够做的事情,原则上,爬虫都能够做到. 2 ...

  8. python之爬取网页数据总结(一)

    今天尝试使用python,爬取网页数据.因为python是新安装好的,所以要正常运行爬取数据的代码需要提前安装插件.分别为requests    Beautifulsoup4   lxml  三个插件 ...

  9. Python实训day07pm【Selenium操作网页、爬取数据-下载歌曲】

    练习1-爬取歌曲列表 任务:通过两个案例,练习使用Selenium操作网页.爬取数据.使用无头模式,爬取网易云的内容. ''' 任务:通过两个案例,练习使用Selenium操作网页.爬取数据. 使用无 ...

随机推荐

  1. plsql中查看sql执行计划

    想要优化sql语句,可以从sql执行计划入手. 在plsql客户端,提供了一个方便的按钮来查看执行计划 选中需要查看的sql语句,点击此按钮,就可以看到该条语句的执行计划了. 结果集包括描述,用户,对 ...

  2. MVC5应用程序生命周期lifecycle

  3. Quartus II 14.0正式版 下载链接和破解器

    Windows版本 必装组件: Quartus II http://download.altera.com/akdlm/software/acdsinst/14.0/200/ib_installers ...

  4. jQuery bind() live()

    <script type="text/javascript"> $(document).ready(function () { /*$('.clickme').live ...

  5. js Array操作

    JS中数组的操作 1.数组的创建 var arrayObj = new Array(); //创建一个数组 var arrayObj = new Array([size]); //创建一个数组并指定长 ...

  6. Promise超时情况

    export const ERROR_PROMISE_TIMEOUT = 'ERROR_PROMISE_TIMEOUT'; export default function (promise, time ...

  7. F - ACboy needs your help again! (模拟)

    ACboy was kidnapped!! he miss his mother very much and is very scare now.You can't image how dark th ...

  8. python3好用的requests库

    python3好用的requests库 requests是什么? requests是基于urllib编写的http库,支持python3,比urllib更好用,更简单.之前使用python写一些htt ...

  9. .NET clickonce修改发布名称等

    见图

  10. 配置Java web的一次经历

    最近在完成数据库作业,重新拾起了以前学过的Java,讲下自己的 Java web 配置过程. 1.安装 Tomcat 在官网下载 Tomcat7.0版本:https://tomcat.apache.o ...