使用Python爬虫批量抓取PubChem化合物信息

在这里插入图片描述

我本科是学生物工程的,研究生转到经济学,但是周围也有很多学生物和医学方面的朋友,经常帮他们抓取一些数据。最近帮他们抓取pubchem上的一些数据,pubchem是一个开放的数据库,爬起来难度不是很大,网上也有一个库叫pubchempy,之前也用来抓取过化合物的结构信息。今天主要是来看看如何通过网站的api来自定义抓取。

首先打开网址:https://pubchem.ncbi.nlm.nih.gov/

通过cid来搜索,输入9809

在这里插入图片描述
在这里插入图片描述
进去之后获得这个地址:https://pubchem.ncbi.nlm.nih.gov/compound/9809

比如我们想抓取化合物的Chemical and Physical Properties信息,点击右侧的Chemical and Physical Properties,然后点击全屏。
在这里插入图片描述
进入到下面这个页面后,右键 检查(推荐使用谷歌浏览器)。
在这里插入图片描述
然后点击 Network,之后刷新页面。
在这里插入图片描述
然后在里面找到一个带有?heading的连接,点开看看,就可以找到数据的api地址,然后复制出来,新开一个页面打开。
在这里插入图片描述
在这里插入图片描述
这个页面里面的数据就是该化合物的Chemical and Physical Properties数据,这个json的层特别多,需要慢慢解析,具体看代码。

import pandas as pd 
import numpy as np  
import json 
import requests 
import time 
 
 
cid = 9809 
url = f'https://pubchem.ncbi.nlm.nih.gov/rest/pug_view/data/compound/{cid}/JSON/?heading=Chemical+and+Physical+Properties' 
 
req = requests.get(url) 
req 
 
proper_json = json.loads(req.text) 
 
proper_json['Record']['Section'][0]['Section'][0]['Section'][2] 
 
Section  = proper_json['Record']['Section'][0]['Section'][1]['Section'] 
 
for i in range(len(Section)): 
    print(Section[i]['TOCHeading'], ': ', Section[i]['Information'][0]['Value']['StringWithMarkup'][0]['String']) 
 
# Physical Description :  4-chloro-3,5-dinitro-alpha,alpha,alpha-trifluorotoluene appears as yellow crystals. (NTP, 1992) 
# Melting Point :  133 to 136 °F (NTP, 1992) 
# LogP :  2.5 (LogP) 

如果有很多cid,那写个循环再加个解析存为excel格式就好了。

简单案例如下:
在这里插入图片描述
如果需要PubChem批量抓取数据服务的同学可以Q我,977728597。

或者到这里:

Puchem化合物数据批量抓取采集_宝典_教程_Python爬虫
在这里插入图片描述

  • 2
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Python风控

支持他!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值