当前位置: 首页 > news >正文

爬虫大作业

1.选一个自己感兴趣的主题。

2.用python 编写爬虫程序,从网络上爬取相关主题的数据。

3.对爬了的数据进行文本分析,生成词云。

4.对文本分析结果进行解释说明。

5.写一篇完整的博客,描述上述实现过程、遇到的问题及解决办法、数据分析思想及结论。

6.最后提交爬取的全部数据、爬虫及数据分析源代码。

 

我所爬取的是校园新闻信息还有多玩LOL新闻版块的新闻

完成作业遇到的问题:

词云wordcloud的安装.

主要是不会怎么借助词云导出。

爬取校园新闻信息并且生成词云

 
  
import requests
import string
import re
import jieba
import matplotlib.pyplot as plt
from wordcloud import WordCloud,STOPWORDS,ImageColorGenerator
from datetime import datetime
newsurl='http://news.gzcc.cn/html/xiaoyuanxinwen/'
res = requests.get(newsurl) #返回response对象
res.encoding='utf-8'
from bs4 import BeautifulSoup
soup = BeautifulSoup(res.text,'html.parser')

def getKeynews(content):
content = ''.join(re.findall('[\u4e00-\u9fa5]', content)) # 通过正则表达式选取中文字符数组,拼接为无标点字符内容
# 去掉重复的字符生成集合
newSet = set(jieba._lcut(content)) #划分内容
newDict = {} #定义字典
for i in newSet:
newDict[i] = content.count(i)
deleteList, keynews = [], []
for i in newDict.keys():
if len(i) < 2:
deleteList.append(i) #去掉单音无意义字符
for i in deleteList:
del newDict[i]
dictList = list(newDict.items())
dictList.sort(key=lambda item: item[1], reverse=True) # 排序
for dict in dictList:
keynews.append(dict[0])
return keynews
def writeFilekeynews(keywords):
f = open('Filekeynews', 'a', encoding='utf-8')
for word in keywords:
f.write(" "+word)
f.close()
def writeNewsDetail(content):
f=open('gzccNews.txt','a',encoding='utf-8')
f.write("\n" + content)
f.close()
def getNewsDetail(newsUrl):
resd = requests.get(newsUrl)
resd.encoding = 'utf-8'
soupd = BeautifulSoup(resd.text, 'html.parser')
content=soupd.select('.show-content')[0].text.strip()
writeNewsDetail(content)
keynews = getKeynews(content)
writeFilekeynews(keynews)
# def getWordCloud():
# keynewsTowordcloud = open('keyword.txt', 'r', encoding='utf-8').read()
# print(keynewsTowordcloud)
# backgroud_Image = plt.imread('bg.jpg')
# wc = WordCloud(background_color='white', # 设置背景颜色
# mask=backgroud_Image, # 设置背景图片
# stopwords=STOPWORDS,
# max_words=80, # 设置最大现实的字数
# font_path='C:\Windows\Fonts\AdobeKaitiStd-Regular.otf', # 设置字体格式,如不设置显示不了中文
# max_font_size=80, # 设置字体最大值
# random_state=30, # 设置有多少种随机生成状态,即有多少种配色方案
# )
# wc.generate(keynewsTowordcloud)
# image_colors = ImageColorGenerator(backgroud_Image)
# wc.recolor(color_func=image_colors)
# plt.imshow(wc)
# plt.axis('off')
# plt.show()

def getListPage(listPageUrl):
res=requests.get(listPageUrl)
res.encoding='utf-8'
soup=BeautifulSoup(res.text,'html.parser')
for news in soup.select('li'):
if len(news.select('.news-list-title'))>0:
a=news.select('a')[0].attrs['href']
getNewsDetail(a)
firstPage='http://news.gzcc.cn/html/xiaoyuanxinwen/'
getListPage(firstPage)
for i in range(2,3):
listpageUrl='http://news.gzcc.cn/html/xiaoyuanxinwen/{}.html'.format(i)
f=open('keyword.txt','r').read()
wordcloud=WordCloud(background_color="white",width=1000,height=860,margin=2,font_path='C:\Windows\Fonts\AdobeKaitiStd-Regular.otf').generate(f)
plt.imshow(wordcloud)
plt.axis("off")
plt.show()
wordcloud.to_file('111.png')
 

 爬取多玩LOL新闻版块

# -*- coding : UTF-8 -*-
import requests
import string
import re
import jieba
import matplotlib.pyplot as plt
from wordcloud import WordCloud,STOPWORDS,ImageColorGenerator
from datetime import  datetime
from bs4 import BeautifulSoup

# def getKeynews(content):
#     content = ''.join(re.findall('[\u4e00-\u9fa5]', content))  # 通过正则表达式选取中文字符数组,拼接为无标点字符内容
#     # 去掉重复的字符生成集合
#     newSet = set(jieba._lcut(content))               #划分内容
#     newDict = {}            #定义字典
#     for i in newSet:
#         newDict[i] = content.count(i)
#     deleteList, keynews = [], []
#     for i in newDict.keys():
#         if len(i) < 2:
#          deleteList.append(i)  #去掉单音无意义字符
#     for i in deleteList:
#         del newDict[i]
#     dictList = list(newDict.items())
#     dictList.sort(key=lambda item: item[1], reverse=True)  # 排序
#     for dict in dictList:
#         keynews.append(dict[0])
#     return keynews
# def writeFilekeynews(keywords):
#     f = open('Filekeynews6', 'a', encoding='utf-8')
#     for word in keywords:
#         f.write(" "+word)
#     f.close()
# def writeNewsDetail(content):
#     f=open('duowanNews.txt','a',encoding='utf-8')
#     f.write("\n" + content)
#     f.close()
# def getNewsDetail(newsUrl):
#     resd = requests.get(newsUrl)
#     resd.encoding = 'utf-8'
#     soupd = BeautifulSoup(resd.text, 'html.parser')
#     content=soupd.select('.show-content')[0].text.strip()
#     writeNewsDetail(content)
#     keynews = getKeynews(content)
#     writeFilekeynews(keynews)


# firstPage='http://lol.duowan.com/tag/172578469745.html'
# for i in range(2,3):
#     listpageUrl='http://news.gzcc.cn/html/xiaoyuanxinwen/{}.html'.format(i)
# f=open('keyword.txt','r').read()
# wordcloud=WordCloud(background_color="white",width=1000,height=860,margin=2,font_path='C:\Windows\Fonts\AdobeKaitiStd-Regular.otf').generate(f)
# plt.imshow(wordcloud)
# plt.axis("off")
# plt.show()
# wordcloud.to_file('111.png')

# f=open('keyword2.txt','r').read()
# wordcloud=WordCloud(background_color="white",width=1000,height=860,margin=2,font_path='C:\Windows\Fonts\AdobeKaitiStd-Regular.otf').generate(f)
# plt.imshow(wordcloud)
# plt.axis("off")
# plt.show()
def write_news_to_document(filename, content):
    f = open(filename, 'w', encoding='utf-8')
    for detail in content:
        f.write(detail['content'])
    f.close()


# 将得到的关键词写入文件
def write_keywords_to_document(filename, keywords):
    f = open(filename, 'w', encoding='utf-8')
    for word in keywords:
        f.write('  ' + word)
    f.close()


# 通过jieba分词得到关键词
def get_keywords(filename):
    f = open(filename, 'r', encoding='utf-8')
    content = f.read()
    f.close()
    word_set = set(jieba.lcut(''.join(re.findall("[\u4e00-\u9fa5_a-zA-Z0-9]", content))))
    # 通过正则表达式选取中文,字母及数字字符数组,拼接为无标点字符内容,再转换为字符集合
    word_dict = {}
    delete_list = []
    keywords = []
    for a in word_set:
        word_dict[a] = content.count(a)  # 生成词云字典
    for j in word_dict.keys():
        if len(j) < 2:
            delete_list.append(j)  # 生成单字无意义字符列表
    for k in delete_list:
        del word_dict[k]  # 在词云字典中删除无意义字符
    dict_list = list(word_dict.items())
    dict_list.sort(key=lambda item: item[1], reverse=True)
    for dict in dict_list:
        keywords.append(dict[0])
    print(keywords)
    write_keywords_to_document("NewsKeyword.txt", keywords)


# 获取详细新闻内容
# def get_news_detail(news_url):
#     res_d = requests.get(news_url)
#     res_d.encoding = 'gbk'
#     soup_d = BeautifulSoup(res_d.text, 'html.parser')
#     content = ''
#     for p in range(0, len(soup_d.select(".text"))):
#         content += soup_d.select('.text')[p].text + '\n'
#     detail = {'content': content}
#     return detail

def get_news_detail(news_url):
    res_d = requests.get(news_url)
    res_d.encoding = 'UTF-8'
    soup_d = BeautifulSoup(res_d.text, 'html.parser')
    content = ''
    for i in range(3, 15):
        content += (soup_d.select('p')[i].text)+ '\n'
    detail = {'content': content}
    return detail




# 获取新闻列表
def get_news_list(list_url):
    res = requests.get(list_url)
    res.encoding = 'utf-8'
    soup = BeautifulSoup(res.text, 'html.parser')
    page_detail = []
    for newsList in soup.select('.m-list')[0].select('li'):
        a = newsList.select('a')[0].attrs['href']
        b = 'http://lol.duowan.com/' + a
        news_detail = get_news_detail(b)
        page_detail.append(news_detail)
    return page_detail


#主函数
 url = "http://lol.duowan.com/tag/172578469745.html"
 Page_detail = get_news_list(url)
 print(Page_detail)
 write_news_to_document("News.txt", Page_detail)
 for i in range(2, 9):
     news_url = "http://lol.duowan.com/tag/172578469745_{}.html".format(i)
     Page_detail = get_news_list(url)
     write_news_to_document("News.txt", Page_detail)
 get_keywords("News.txt")
f=open('NewsKeyword.txt','r').read()
wordcloud=WordCloud(background_color="white",width=1000,height=860,margin=2,font_path='C:\Windows\Fonts\AdobeKaitiStd-Regular.otf').generate(f)
plt.imshow(wordcloud)
plt.axis("off")
plt.show()
wordcloud.to_file('777.png')

爬取这段的时候是LOL的MSI季中赛决赛结束后夺冠,可以看出RNG战队的UZI、Letme、Karsa、Ming、Xiaohu、Mlxg这几名选手的热度都很高,然后由于UZI、Letme、Karsa,这三名选手的表脸都很亮眼,所以词频数很高。

 

转载于:https://www.cnblogs.com/swxvico/p/8973164.html

相关文章:

  • 散列表解决冲突的办法
  • 访谈:摩尔定律后时代,看13位行业专家如何看量子计算机的未来?
  • 青云QingCloud黄允松:关于云计算未来的三个预测
  • slim.flatten——将输入扁平化但保留batch_size,假设第一维是batch
  • 深入浅出MyBatis:MyBatis插件及开发过程
  • 解决Mybatis配置ORM映射 时分秒都为0
  • Spring Cloud入门教程-Hystrix断路器实现容错和降级
  • 0505 php-数组、控制语句、函数
  • 第三期 行为规划——6.输出状态转换方程的量
  • Ping程序
  • 群发功能推广通知短信的一些问题
  • 蓝海存储开关机注意事项总结
  • Fragment向父Activity传值
  • jmeter学习笔记
  • 债券和股票
  • [分享]iOS开发-关于在xcode中引用文件夹右边出现问号的解决办法
  • Docker: 容器互访的三种方式
  • ES6语法详解(一)
  • ESLint简单操作
  • Java的Interrupt与线程中断
  • overflow: hidden IE7无效
  • 测试开发系类之接口自动化测试
  • 从0实现一个tiny react(三)生命周期
  • 对话:中国为什么有前途/ 写给中国的经济学
  • 多线程 start 和 run 方法到底有什么区别?
  • 翻译--Thinking in React
  • 数据结构java版之冒泡排序及优化
  • 我有几个粽子,和一个故事
  • 我这样减少了26.5M Java内存!
  • 由插件封装引出的一丢丢思考
  • 终端用户监控:真实用户监控还是模拟监控?
  • Redis4.x新特性 -- 萌萌的MEMORY DOCTOR
  • # 日期待t_最值得等的SUV奥迪Q9:空间比MPV还大,或搭4.0T,香
  • #《AI中文版》V3 第 1 章 概述
  • #HarmonyOS:软件安装window和mac预览Hello World
  • #NOIP 2014# day.1 T3 飞扬的小鸟 bird
  • (02)Hive SQL编译成MapReduce任务的过程
  • (附源码)spring boot基于小程序酒店疫情系统 毕业设计 091931
  • (免费分享)基于springboot,vue疗养中心管理系统
  • (一)eclipse Dynamic web project 工程目录以及文件路径问题
  • (原創) 如何安裝Linux版本的Quartus II? (SOC) (Quartus II) (Linux) (RedHat) (VirtualBox)
  • ***通过什么方式***网吧
  • .NET Framework .NET Core与 .NET 的区别
  • .NET I/O 学习笔记:对文件和目录进行解压缩操作
  • .Net 中Partitioner static与dynamic的性能对比
  • :O)修改linux硬件时间
  • @Autowired注解的实现原理
  • [2009][note]构成理想导体超材料的有源THz欺骗表面等离子激元开关——
  • [ANT] 项目中应用ANT
  • [CareerCup] 12.3 Test Move Method in a Chess Game 测试象棋游戏中的移动方法
  • [Codeforces1137D]Cooperative Game
  • [CSS]中子元素在父元素中居中
  • [CVPR 2023:3D Gaussian Splatting:实时的神经场渲染]
  • [error] 17755#0: *58522 readv() failed (104: Connection reset by peer) while reading upstream
  • [excel与dict] python 读取excel内容并放入字典、将字典内容写入 excel文件