python实现的读取网页并分词功能示例-创新互联

本文实例讲述了python实现的读取网页并分词功能。分享给大家供大家参考,具体如下:

创新互联公司是专业的贵定网站建设公司,贵定接单;提供网站建设、做网站,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行贵定网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!

这里使用分词使用最流行的分词包jieba,参考:https://github.com/fxsjy/jieba

或点击此处本站下载jieba库

代码:

import requests
from bs4 import BeautifulSoup
import jieba
# 获取html
url = "http://finance.ifeng.com/a/20180328/16049779_0.shtml"
res = requests.get(url)
res.encoding = 'utf-8'
content = res.text
# 添加至bs4
soup = BeautifulSoup(content, 'html.parser')
div = soup.find(id = 'main_content')
# 写入文件
filename = 'news.txt'
with open(filename,'w',encoding='utf-8') as file_object:
  # 

标签的处理 for line in div.findChildren(): file_object.write(line.get_text()+'\n') # 使用分词工具 seg_list = jieba.cut("我来到北京清华大学", cut_all=True) print("Full Mode: " + "/ ".join(seg_list)) # 全模式 seg_list = jieba.cut("我来到北京清华大学", cut_all=False) print("Default Mode: " + "/ ".join(seg_list)) # 精确模式 seg_list = jieba.cut("他来到了网易杭研大厦") # 默认是精确模式 print(", ".join(seg_list)) with open(filename,'r',encoding='utf-8') as file_object: with open('cut_news.txt','w',encoding='utf-8') as file_cut_object: for line in file_object.readlines(): seg_list = jieba.cut(line,cut_all=False) file_cut_object.write('/'.join(seg_list))

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


标题名称:python实现的读取网页并分词功能示例-创新互联
转载注明:http://bzwzjz.com/article/hpeds.html

其他资讯

Copyright © 2007-2020 广东宝晨空调科技有限公司 All Rights Reserved 粤ICP备2022107769号
友情链接: 成都商城网站制作 成都品牌网站设计 成都响应式网站建设公司 网站设计公司 网站设计 网站建设公司 企业网站设计 重庆网站建设 成都网站设计 高端定制网站设计 定制网站建设 手机网站制作 营销型网站建设 移动手机网站制作 成都网站建设 成都定制网站建设 古蔺网站建设 成都网站建设流程 外贸营销网站建设 做网站设计 泸州网站建设 网站设计