爬虫大作业

1.选一个自己感兴趣的主题(所有人不能雷同)。

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

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

import matplotlib.pyplot as plt
from wordcloud import WordCloud,ImageColorGenerator,STOPWORDS
import jieba
import numpy as np
from PIL import Image

#读入背景图片
abel_mask = np.array(Image.open("lzl.jpg"))

#读取要生成词云的文件
text_from_file_with_apath = open('outcome.txt',encoding='utf-8').read()

#通过jieba分词进行分词并通过空格分隔
wordlist_after_jieba = jieba.cut(text_from_file_with_apath, cut_all = True)
wl_space_split = " ".join(wordlist_after_jieba)

my_wordcloud = WordCloud(
            background_color='white',    
            mask = abel_mask,       
            max_words = 500,            
            stopwords = {}.fromkeys(['nbsp','br']),       
            max_font_size = 450,           
            random_state = 30,            
                scale=.5
                ).generate(wl_space_split)

# 根据图片生成词云颜色
image_colors = ImageColorGenerator(abel_mask)

# 以下代码显示图片
plt.imshow(my_wordcloud)
plt.axis("off")
plt.show()

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

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

只是在喜欢的网页中找到一篇比较热的文章进行简单的爬取和词云分析

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

import requests
from bs4 import BeautifulSoup

url = 'https://www.theatlantic.com/science/archive/2018/04/dont-be-afraid-of-the-multiverse/559169/'
res = requests.get(url)
res.encoding = 'utf-8'
soup = BeautifulSoup(res.text, 'html.parser')
#print(soup)
news = soup.select('.article-body')[0].text
def saveNews(content):
    f = open("articlr.txt", 'a', encoding='utf-8')
    f.write(content)
    f.close()
"""
for new in news:
    title = new.select('.article-section-1')[0].text
    href = new.select('a')[0].attrs['href']
    print(title)
    print(href)
#for i in range(1,15):
"""
print(news)
原文地址:https://www.cnblogs.com/REGzjm85/p/8974537.html