# 载入停用词列表 # 停用词链接:https://github.com/goto456/stopwords with open(r'cn_stopwords.txt','r',encoding='utf8') as f: stopwords=f.readlines() stop_list=[w.strip() for w in stopwords]
# jieba 进行分词 word_cut=jieba.cut(text) word_result=[w.strip() for w in word_cut if w notin stop_list and len(w.strip())>0and not_digit(w)] print(word_result)
# 再次进行分词 word_cut=jieba.cut(text) word_result=[w.strip() for w in word_cut if
w notin stop_list and len(w.strip())>0and not_digit(w)] print(word_result)
# 词库大类名称的xpath路径 first_name_xpath="//div[@class='dict_category_list_title']/a/text()|//div\ [@class='dict_category_list_title ']/a/text()" first_name_list=[name for name in tree.xpath(first_name_xpath)]
# 页面链接的 xpath 路径 first_href_xpath="//div[@class='dict_category_list_title']/a/@href|//div\ [@class='dict_category_list_title ']/a/@href" first_href_list=[href for href in tree.xpath(first_href_xpath)]
# 对页面进行循环 for page in range(1,page_num+1): url=basic_url+str(page) print(f'第{page}页') # 防止timeout导致报错 sourcecode=True while sourcecode: try: html=requests.get(url,headers=headers,timeout=10) sourcecode=False except: time.sleep(10) html=requests.get(url,headers=headers) html=html.text tree=etree.HTML(html) name_xpath="//div[@class='detail_title']/a/text()" # 存在“ ”等不规范的文件命名字符,将其替换为空 name_list=[re.sub("\\|\/|","",name) for name in tree.xpath(name_xpath)] download_xpath="//div[@class='dict_dl_btn']/a/@href" download_list=[link for link in tree.xpath(download_xpath)] all_name.extend(name_list) all_download.extend(download_list) print(f'{first_name_list[i]}完成')
通过以上代码,我们爬取到了所有的词库名称和下载链接。
3.3 下载细胞词库
循环下载链接列表,将词库名称作为文件名下载 scel 文件。
# 设置文件存放路径 basic_storepath=r'D:\分词' for i in range(len(all_name)): storepath=basic_storepath+all_name[i] down_link=all_download[i]
# 防止timeout导致报错 sourcecode=True while sourcecode: try: html=requests.get(down_link,headers=headers,timeout=10) sourcecode=False except: time.sleep(10)
r=requests.get(down_link,headers=headers,timeout=10) with open (f"{storepath}.scel",'wb') as f: f.write(r.content) print(f'第{i}个{all_name[i]}下载完成')
# 对所有文件进行循环 for i in range(len(files)): try: GTable=[] o=[files[i]] for f in o: deal(f) # 定义转化后的txt文档所在路径 with open (f'F:\\搜狗词库txt\\{files[i][:-5]}.txt','w',encoding='utf8') as f: for word in GTable: f.write(word[2]+'\n') print(f"第{i}个文件已转化") except: print(f'{i}个文件有问题') continue
运行以上代码,最后成功转化了 5756 份文件。
5. 相关推文
Note:产生如下推文列表的 Stata 命令为: lianxh python, m 安装最新版 lianxh 命令: ssc install lianxh, replace
专题:数据分享
Python+Stata:如何获取中国气象历史数据
专题:Stata入门
使用 Jupyter Notebook 配置 Stata\Python\Julia\R
专题:文本分析-爬虫
Python:计算管理层讨论与分析的余弦相似度
Stata+Python:爬取创历史新高股票列表
Python:爬取东方财富股吧评论进行情感分析
Python爬虫: 《经济研究》研究热点和主题分析
专题:Python-R-Matlab
Python:Jaccard 相似度和距离
Python:多进程、多线程及其爬虫应用
Python:爬取动态网站
Python爬取静态网站:以历史天气为例
Python:爬取巨潮网公告
司继春:Python学习建议和资源
Python:爬取上市公司公告-Wind-CSMAR
Python: 6 小时爬完上交所和深交所的年报问询函
Python: 使用正则表达式从文本中定位并提取想要的内容
Python: 批量爬取下载中国知网(CNKI) PDF论文
New! Stata 搜索神器:lianxh 和 songblGIF 动图介绍 搜: 推文、数据分享、期刊论文、重现代码 …… 👉 安装: . ssc install lianxh . ssc install songbl 👉 使用: . lianxh DID 倍分法 . songbl all
课程推荐:Stata2R:针对 Stata 用户的 R 课程 主讲老师:游万海 (福州大学) 课程时间:2023 年 12 月 3/10/17 (三个周日) 🍓 课程主页:https://www.lianxh.cn
New! Stata 搜索神器:lianxh 和 songblGIF 动图介绍 搜: 推文、数据分享、期刊论文、重现代码 …… 👉 安装: . ssc install lianxh . ssc install songbl 👉 使用: . lianxh DID 倍分法 . songbl all