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

在爬取时遇到了最大的困难是有些大型网页内容不让爬取,以及大型网页的内容的类不一定会一样,难以批量爬取。

现在爬取的网页信息为http://news.17173.com/

 

2.网络上爬取相关的数据。

from datetime import datetime
import requests
from bs4 import BeautifulSoup
html='http://news.17173.com/'
res = requests.get(html)
res.encoding='utf-8'
soup = BeautifulSoup(res.text,'html.parser')
def getdetail(htm):
    resd = requests.get(htm)
    resd.encoding='utf-8'
    soupd = BeautifulSoup(resd.text,'html.parser')
    return soupd.select('.gb-final-mod-article.gb-final-mod-article-p2em')[0].text
for news in soup.select('li'):
        if len(news.select('.item-con'))>0:
         print (news.select('.item-con')[0].text)
         htm = news.select('a')[0]['href']
         print (news.select('a')[0]['href'])
         detail =  getdetail(htm)
         print(detail)
      

 该游戏新闻中心爬取结果

 

3.进行文本分析,生成词云。

import jieba
book = "wlh.txt"
txt = open(book,"r",encoding='utf-8').read()
ex = {'美女','官方','10','11','31http','news.17173','com','content','10312017'}

words = jieba.cut(txt)
counts = {}
for word in words:
    if len(word) == 1:
        continue
    else:
        counts[word] = counts.get(word,0) + 1
for word in ex:
    del(counts[word])
items = list(counts.items())
items.sort(key=lambda x:x[1],reverse=True)
for i in range(20):
    word,count = items[i]
    print("{0:<8}{1:>4}".format(word,count))

文本分析

对爬取的内容进行词云分析

import re
import jieba
import matplotlib.pyplot as plt
from wordcloud import WordCloud
file=open('wlh.txt','r',encoding='utf-8').readlines()

data=''
for i in file:
    data+=' '.join(jieba.cut(i))+' '

my_wordcloud = WordCloud(font_path='simheittf.ttf').generate(data)
plt.imshow(my_wordcloud)
plt.axis("off")
plt.show()

 

 

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

 对游戏新闻爬取的数据得到一些关于游戏行业方面的信息,说明在游戏行业当中,一款游戏是否成功,玩家是最重要的因素。

posted on 2017-11-01 00:25  吴林鸿  阅读(125)  评论(0编辑  收藏  举报