某位中医站长找到狗哥,希望能够采集2020版药典里的中药相关数据,以便添加到自己的网站中,实现按照性味归经、主治功能、用法用量、注意事项等信息进行多重筛选。如果能有一个比较全面的中药材数据库,那么中医开药方的时候就能有多种选择,而不是靠自己的记忆力开药方。
首先需要安装python库
复制
pip3 install pandas pip3 install Pypinyin pip3 install beautifulsoup4 pip3 install requests
然后复制下面的代码执行即可
复制
import requests,time
from bs4 import BeautifulSoup
from pypinyin import lazy_pinyin, Style
import pandas as pd
#添加拼音缩写
def add_initials_to_chinese_name(name):
if not name:
return name
initials = lazy_pinyin(name, style=Style.FIRST_LETTER)
initials_str = ''.join(initials).lower()
modified_name = f"{initials_str}-{name}"
return modified_name
response=requests.get(url="https://ydz.chp.org.cn/front-api/children?bookId=1&directoryId=9")
data=response.json()
yc=None
ycdata=[]
if data['code']==200:
yc=data['data']['children']
excel_path = 'output.xlsx'
for item in yc:
oneyc=[]
herb_name=item['title']
modified_herb_name = add_initials_to_chinese_name(herb_name)
print(modified_herb_name)
oneyc.append(modified_herb_name)
response=requests.get(url="https://ydz.chp.org.cn/front-api/entry/"+str(item['id']))
if response.status_code==200:
content=response.json()
if content['code']==200:
html_doc=content['data']['htmlContent']
soup = BeautifulSoup(html_doc, 'html.parser')
# 查找【性味与归经】的内容
try:
# 尝试寻找【性味与归经】
xing_wei_gui_jing = soup.find('p', string='【性味与归经】').find_next_sibling('p').text
except AttributeError:
try:
# 如果上面的寻找失败,则尝试寻找性味
xing_wei_gui_jing = soup.find('p', string='【性味】').find_next_sibling('p').text
except AttributeError:
# 如果两次尝试都失败了,则设置一个默认值
xing_wei_gui_jing = '自行替换'
# 查找【功能与主治】的内容
try:
gong_neng_zhu_zhi = soup.find('p', string='【功能与主治】').find_next_sibling('p').text
except AttributeError:
gong_neng_zhu_zhi="没有功能与主治"
try:
yong_fa_yong_liang = soup.find('p', string='【用法与用量】').find_next_sibling('p').text
except AttributeError:
yong_fa_yong_liang="没有用法与用量"
try:
zhu_yi = soup.find('p', string='【注意】').find_next_sibling('p').text
except AttributeError:
zhu_yi="没有注意事项"
print(xing_wei_gui_jing)
print(gong_neng_zhu_zhi)
print(yong_fa_yong_liang)
print(zhu_yi)
oneyc.append(xing_wei_gui_jing)
oneyc.append(gong_neng_zhu_zhi)
oneyc.append(yong_fa_yong_liang)
oneyc.append(zhu_yi)
ycdata.append(oneyc)
time.sleep(1)
result = pd.DataFrame(ycdata, columns=['名称', '性味', '功能','法量','注意'])
result.to_csv("C:\\Users\daimadog\Desktop\ycdata.csv", index=False)
else:
print("error")
采集完成后会将所有数据写入到excel文件中。药典2020中药饮片类总共615种药材,由于国家网站使用了接口实现数据交互,所以整个采集过程还是非常轻松的,并且没有任何防止采集措施,想必这玩意儿应该也没几个人采集吧。





评论 (0)