文档第一页加入作者信息_Python爬取中药网站信息并对其进行简单的分析

开发工具

  • Python版本:3.5.4
  • 相关模块:
03bbf2029adb931591e47fce1f7c2cdc.png

爬虫:

import requestsfrom bs4 import BeautifulSoup

词云:

from wordcloud import WordCloudimport jiebafrom os import pathimport matplotlib.pyplot as plt

主要思路:

写了两个文件,具体如下:

1).数据爬取并生成txt文件的py文件

2).利用python相关的包生成词云相关操作的py文件

(2).遇到的问题以及解决方案:

wordcloud包的安装配置出现很大的问题,本机系统装载了两个python版本导致装载出现很多额外的问题。

  • 解决:在同学的帮助下安装了whl文件并删除了本机中的另一个python版本。

信息爬取过慢

  • 解决:暂未解决。爬取的页面预计超过100p,所以有关方面可能需要依赖别的技术。
d5ec3669a28ebb066cb5a88af83f5828.png

使用演示

程序运行截图:

b617f9c6a383a1aa56ddec310ddf9d56.png

导出文档:

38e2b0199c0dbe943dca2ff471afc38b.png

数据爬取并生成txt文件的py文件:

import requestsfrom bs4 import BeautifulSoup'''遇到不懂的问题?Python学习交流群:821460695满足你的需求,资料都已经上传群文件,可以自行下载!'''#获取——————————————————————————————————————————def catchSoup(url): #url='http://www.18ladys.com/post/buchong/' res=requests.get(url) res.encoding='utf-8' soup=BeautifulSoup(res.text,'html.parser') return soup​#类型及其网页查找(首页查找)——————————————————————def kindSearch(soup): herbKind=[] for new in soup.select('li'): if(new.text!='首页'): perKind=[] perKind.append(new.text) perKind.append(new.select('a')[0].attrs['href']) herbKind.append(perKind) return herbKind​#药名查找(传入页面)——————————————————————————————————————————————————————def nameSearch(soup): herbName=[] for new in soup.select('h3'): pername=new.text.split('_')[0].rstrip('图片').lstrip('xa0').split('的功效')[0].split('(')[0].split('功效')[0].rstrip('的') herbName.append(pername) return herbName​#分页及详细地址——————————————————————————————————————————————————————————def perPage(soup): kindPage=[] add=[] for new in soup.select('.post.pagebar'): for detail in new.select('a'): d=[] d.append(detail.text) d.append(detail.attrs['href']) kindPage.append(d) kindPage.remove(kindPage[0]) kindPage.remove(kindPage[-1]) return kindPage#爬取某一类的所有药名:kind是一个数字,照着kindSearch的结果输入。————————————def herbDetail(kind): soup=catchSoup('http://www.18ladys.com/post/buchong/')#从首页开始 kindName=kindSearch(soup)[kind][0] #这一类草药的类名 adds=kindSearch(soup)[kind][1] #这一类草药的第一页地址 totalRecord = [] #这一类草药的所有名字 print("正在爬取 "+str(kind)+'.'+kindName) totalRecord.append(nameSearch(catchSoup(adds)))#第一页的草药 for add in perPage(catchSoup(adds)): #第二页以及之后的草药 pageAdd=add[1] totalRecord.append(nameSearch(catchSoup(pageAdd))) #print(nameSearch(catchSoup(pageAdd))) print(totalRecord) return totalRecord​#===========================================================# 操作#===========================================================if __name__=="__main__": #获取类别名字及其网页地址— totalKind=kindSearch(catchSoup('http://www.18ladys.com/post/buchong/')) #首页 #获取某一类中药的各种药名 kind=0 detailContent = '' while(kind<20): #如果要爬取全网站请写41 index = 1 #前面的序列号指示 totalRecord = [] totalRecord=herbDetail(kind) if(kind==0): detailContent+='目录:' for i in totalKind: detailContent+=str(index)+'.'+i[0]+' ' index+=1 kind+=1 continue else: detailContent+=''+str(totalKind[kind-1][0])+':' for i in totalRecord: k=0 while k

词云生成部分

from wordcloud import WordCloudimport jiebafrom os import pathimport matplotlib.pyplot as plt'''遇到不懂的问题?Python学习交流群:821460695满足你的需求,资料都已经上传群文件,可以自行下载!'''comment_text = open('D:herbDetail.txt','r',encoding='utf-8').read()cut_text = " ".join(jieba.cut(comment_text))d = path.dirname(__file__)cloud = WordCloud( font_path="C:WindowsFontssimhei.ttf
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值