from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
import pandas as pd
from bs4 import BeautifulSoup
import yagmail
from myconfig import mail_user, mail_password
# 设置 Chrome 驱动路径
= '/usr/lib/chromium-browser/chromedriver'
chrome_driver_path
# 设置 Chrome 选项
= Options()
chrome_options '--headless') # 无头模式,不显示浏览器界面
chrome_options.add_argument('--no-sandbox') #加上这个参数才正常启动
chrome_options.add_argument(
# 创建 Chrome WebDriver 实例
= Service(chrome_driver_path)
service = webdriver.Chrome(service=service, options=chrome_options)
driver
## 获取网页html内容
def get_page_content(url, driver=driver):
# 请求网页
driver.get(url)
# 获取完整页面内容
= driver.page_source
page_content return page_content
## 提取html信息
def extract_info_from_page_content(page_content, driver = driver):
### 获取一般信息
= pd.read_html(page_content)[0].drop(columns = '历史版本').drop(columns = "注册题目")
general
### 获取链接
= BeautifulSoup(page_content)
soup = soup.find('table')
table = []
links = []
registration_nos = []
titles
for tr in table.find_all('tr'):
# print(tr)
= tr.find_all('td')
tds if tds:
# print(tds)
= tds[1].text
registration_no = f"https://www.chictr.org.cn/{tds[2].find('a')['href']}"
url = tds[2].find('a')['title']
title
links.append(url)
registration_nos.append(registration_no)
titles.append(title)
= pd.DataFrame({'链接': links,
link_df '注册号':registration_nos,
'注册题目':titles})
### 获取项目负责人信息
= []
registration_nos = []
directors for url in link_df.链接.values:
driver.get(url)# 获取完整页面内容
= driver.page_source
page_content = pd.read_html(page_content)[0]
df1 = df1.iloc[0,1]
registration_no = pd.read_html(page_content)[1]
df2 = df2.iloc[0, 3]
director
registration_nos.append(registration_no)
directors.append(director)
= pd.DataFrame({'项目负责人': directors,
director_df '注册号':registration_nos})
# 将三个表通过registration_no拼接
= general.merge(link_df).merge(director_df)
merge
return merge
## 发送邮件
def send_info(send_info_html):
= yagmail.SMTP(user=mail_user, password= mail_password, host='smtp.163.com')
yag
# 发送邮件
# 多个邮箱地址
= ['hulinhui@live.cn', 'mmkejiaoke@163.com', 'hym202008@163.com']
recipient_list
=recipient_list,
yag.send(to='提醒:茂名市人民医院有新的临床试验注册,需确认项目负责人有无及时备案',
subject= send_info_html)
contents
# 关闭SMTP连接
yag.close()
# 假设有2页内容
= []
registration_table for page_num in range(2):
= f'https://www.chictr.org.cn/searchproj.html?sponsor=茂名市人民医院&page={page_num+1}'
url
= get_page_content(url)
page_content = extract_info_from_page_content(page_content)
registration_info
registration_table.append(registration_info)
# 关闭浏览器
driver.quit()
53 selenium
53.1 安装
53.1.1 安装google浏览器并查看版本
sudo apt install chromium-browser
chromium-browser --version
Chromium 112.0.5615.49 Built on Ubuntu , running on Ubuntu 18.0
4
53.1.2 下载对应版本的 Chrome WebDriver
https://chromedriver.storage.googleapis.com/index.html
https://chromedriver.storage.googleapis.com/112.0.5615.49/chromedriver_linux64.zip
- 下载至pyln文件夹
wget https://chromedriver.storage.googleapis.com/112.0.5615.49/chromedriver_linux64.zip
- 解压至driver文件夹
mkdir driver unzip chromedriver_linux64.zip -d driver
53.2 启动
53.2.1 打包成函数
获取茂名市人民医院临床注册的项目
## 合并与保存
= pd.concat(registration_table, ignore_index = True)
df df
注册号 | 研究类型 | 注册时间 | 链接 | 注册题目 | 项目负责人 | |
---|---|---|---|---|---|---|
0 | ChiCTR2400082051 | 干预性研究 | 2024/03/19 | https://www.chictr.org.cn/showproj.html?proj=2... | 注水法内镜下黏膜切除术与传统黏膜切除术治疗大肠中等大小扁平息肉的对比研究 | 邓奇烽 |
1 | ChiCTR2400080737 | 诊断试验 | 2024/02/05 | https://www.chictr.org.cn/showproj.html?proj=2... | PET/CT靶向肿瘤成纤维细胞分子探针FAPI-04在IA期肺腺癌的应用研究 | 梁汉祥 |
2 | ChiCTR2300078463 | 干预性研究 | 2023/12/08 | https://www.chictr.org.cn/showproj.html?proj=2... | 基于AIDET的协作式术前访视模式构建与应用 | 黄洁莹 |
3 | ChiCTR2300073039 | 干预性研究 | 2023/06/30 | https://www.chictr.org.cn/showproj.html?proj=2... | 转移性结直肠癌患者疗效评估 | 侯雅婷 |
4 | ChiCTR2100054127 | 干预性研究 | 2021/12/09 | https://www.chictr.org.cn/showproj.html?proj=1... | 周疗紫杉醇联合双靶新辅助治疗HER-2 阳性乳腺癌对比WSG-ADAPT HER-2+ 系列... | 申美莹 |
5 | ChiCTR2100051593 | 干预性研究 | 2021/09/28 | https://www.chictr.org.cn/showproj.html?proj=1... | 幽门后喂养预防呼吸机相关性肺炎的有效性及安全性:一项前瞻性、多中心、开放标签、随机对照的临床试验 | 陈纯波 |
6 | ChiCTR2000040543 | 干预性研究 | 2020/12/01 | https://www.chictr.org.cn/showproj.html?proj=5... | 对比瑞戈非尼与呋喹替尼三线治疗结直肠癌的有效性和安全性的平行、随机、对照III期临床试验 | 黄逸生 |
7 | ChiCTR2000039125 | 干预性研究 | 2020/10/17 | https://www.chictr.org.cn/showproj.html?proj=6... | 橘红痰咳液缓解成人社区获得性肺炎症状的前瞻性、多中心、随机对照临床研究 | 陈纯波 |
8 | ChiCTR2000039023 | 干预性研究 | 2020/10/13 | https://www.chictr.org.cn/showproj.html?proj=6... | 硫辛酸注射液治疗脓毒症和脓毒性休克成人患者:一项前瞻性、多中心、单盲、随机、安慰剂对照的临床试验 | 陈纯波 |
9 | ChiCTR2000033745 | 观察性研究 | 2020/06/11 | https://www.chictr.org.cn/showproj.html?proj=5... | 克感利咽口服液对新型冠状病毒肺炎(COVID-19)恢复期、疑似患者及其它易感人群的咽喉部症... | 李耀才 |
10 | ChiCTR2000033102 | 干预性研究 | 2020/05/20 | https://www.chictr.org.cn/showproj.html?proj=5... | 埃克替尼联合贝伐单抗治疗晚期EGFR突变阳性肺腺癌脑转移的临床II期试验 | 黄逸生 |
11 | ChiCTR2000030804 | 干预性研究 | 2020/03/15 | https://www.chictr.org.cn/showproj.html?proj=5... | 化橘红制剂缓解新型冠状病毒肺炎(COVID-19)症状的随机对照临床研究 | 胡林辉 |
12 | ChiCTR2000030555 | 干预性研究 | 2020/03/07 | https://www.chictr.org.cn/showproj.html?proj=5... | 中国南方汉族人强化抗血小板治疗穿支动脉粥样硬化疾病的前瞻性随机对照研究 | 李灏 |
13 | ChiCTR2000030471 | 干预性研究 | 2020/03/02 | https://www.chictr.org.cn/showproj.html?proj=5... | 硫辛酸注射液降低新型冠状病毒肺炎(COVID-19)普通型患者进展风险的有效性及安全性的前瞻... | 陈纯波 |
## 判断有无新注册项目
= pd.read_csv('data/茂名市人民医院临床试验注意清单.csv')['注册号']
old_registration_no
= df['注册号']
new_registration_no
= set(new_registration_no) - set(old_registration_no)
diff_registration_no
# 如果存在新注册,则将信息导出为html文本,通过邮箱发送
if diff_registration_no:
= df[df['注册号'].isin(diff_registration_no)].to_html()
send_info_html send_info(send_info_html)
# 保存数据
'data/茂名市人民医院临床试验注意清单.csv', index=False) df.to_csv(