jieba分词中,有些词不想分,比如 #标签# 比如http://...(URL)应该怎么解决?

使用jieba分词,对新浪微博的数据进行处理,想统计用户的每条微博使用 标签即## 和 使用短连接 URL的频率
可是正常分词之后,类似 #王思聪##权力的游戏# 这样的标签,被分词成 # 王思聪 ## 权利的游戏 #
想保护原有的样子应该怎么办?

​ import requests, time,jieba,wordcloud from bs4 import BeautifulSoup def getHtmlDoc(url):#从指定的 URL 获取网页内容,并将其保存到一个 HTML 文件中,同时提供了简单的错误处理机制。 headers = {#这个信息模拟了一个浏览器,目的是为了避免被网站识别为爬虫。 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36', } try: r = requests.get(url, headers=headers) # 添加headers r.raise_for_status() # 检查是否请求成功 page_text = r.text # 存储 with open(r"D:\Write Code\python\爬虫\comment\xiaowangzi11.html", "w", encoding="utf-8") as f: f.write(page_text) print("存储成功") return page_text except Exception as e: print(f"发生错误: {e}") def getComment(html): comment=[] soup = BeautifulSoup(html, 'html.parser')#使用 BeautifulSoup 库将传入的 HTML 文档解析成一个 soup 对象。这里指定使用 html.parser 作为解析器。 #soup = BeautifulSoup(html, 'lxml')#使用 lxml 库解析 HTML 文档,速度更快。 comment = soup.find_all("p", class_="comment-content") return comment #主程序 #all_comment = [] url = "https://book.douban.com/subject/1084336/comments/?start={p}&limit=20&status=P&sort=score" headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36', } all_comment=[] for p in range(0, 1200, 20): html=getHtmlDoc(url) page_comment = getComment(html) all_comment.extend(page_comment) time.sleep(1) # 延时1秒,防止被禁 print("第{}页评论爬取完成".format(p//20+1)) print(len(all_comment)) # with open("D:\\Write Code\\python\\爬虫\\comment\\小王子评论11.txt", "w", encoding="utf-8") as file: for comment in all_comment:#循环遍历所有评论 file.write(comment.get_text(strip=True) + "\n") # get_text(strip=True) 是 BeautifulSoup 的方法,用于从 comment 中提取纯文本内容。参数 strip=True 表示在提取文本时,会去掉前后空白字符(如空格和换行),使用 get_text() 方法获取文本,并写入文件 ​解析每一句代码。
最新发布
03-27
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值