爬取河科院各个新闻内容,然后分词处理,取出来最高的前十个词(作业)

爬取新闻内容

import urllib.request
from bs4 import BeautifulSoup
response = urllib.request.urlopen('https://news.hist.edu.cn/kyyw/378.htm')
#print(response.read().decode('utf-8'))
content=response.read().decode('utf-8')
soup = BeautifulSoup(content, 'html.parser', from_encoding='utf-8')
# lis=soup.find_all('li')
# for li in lis:
# print(li)
divs=soup.find_all('div',{'class':"sec-a"})
# print(divs)
lis=divs[0].find_all('li')
num = 1

# 获取新闻内容
def content(href,title):
    response = urllib.request.urlopen('https://news.hist.edu.cn/' + href)
    content = response.read().decode('utf-8')
    soup = BeautifulSoup(content, 'html.parser', from_encoding='utf-8')
    divs = soup.find_all('div', {'id': "vsb_content_501"})
    # print(divs)
    if len(divs) == 0:
        return
    ps = divs[0].find_all('p')
	# 写入news目录下 
    file = "./news/" +title + ".txt"
    for p in ps:
        with open(file,'a',encoding='utf-8') as f:
            f.write(p.text + '\n')



for li in lis:
    href = li.find_all('a')[0].get("href")
    if len(href) == 0:
        continue
    title = li.find_all('a')[0].get("title")
    // 一个一个网页爬取
    content(href[2:],title)

分词处理

import jieba
#
# str="好好学习,天天向上"
# print("/".join(jieba.lcut(str)))
#
import os

ciyu ={}

rootdir = './news/'
list = os.listdir(rootdir)  # 列出文件夹下所有的目录与文件
for i in range(0, len(list)):
    path = os.path.join(rootdir, list[i])
    # if os.path.isfile(path):
    # 遍历所有文件
    with open(path,'r',encoding='utf-8') as f:
        data = f.readlines()
        list1 = jieba.lcut(str(data))
        for i in list1:
        	# 去掉小于等于1个的词
            if len(i) <= 1:
                continue
            if ciyu.get(i) == None:
                ciyu[i] = 1
            else:
                ciyu[i] += 1

# 排序
sort_ciyu = sorted(ciyu.items(),key=lambda x:x[1],reverse=True)[:10]
print(sort_ciyu)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值