实验五 爬虫基础

一、实验目的

  1. 掌握爬虫常用的函数

  1. 掌握爬虫的思路

二、实验环境

  1. 操作系统:Windows

  1. 主要软件:Jupyter notebook

三、实验内容

  1. 已知阿司匹林的ChEMBL id 为CHEMBL25,请编写程序爬取阿司匹林的英文名称、分子量、SMILES、并下载分子结构图片。

网址:https://www.ebi.ac.uk/chembl/compound_report_card/CHEMBL25/

编程思路:

(1)首先在网页中找到需要爬取信息对应的位置

(2)查看网页源代码,找到信息所在位置。

(3)编程获取HTML

(4)通过正则表达式匹配对应信息

(5)输出匹配结果

输出结果示例:

  1. 请编写程序用“cancer”关键字搜索pubmed数据库,爬取搜索得到的文献数量,以及前10篇文章的PubMed ID和文章题目。

网址:https://pubmed.ncbi.nlm.nih.gov/

提示:使用urllib.request.urlopen函数获取网页请求;使用req.read().decode()读取网页源代码。

输出结果示例:

四、实验报告

1. 使用jupyter notebook文档填写实验报告,导出并提交pdf格式文件。

文件命名规则:”星期几+学号+姓名+实验5.pdf。

2. 记录实验步骤和实验结果

3. 记录实验中遇到的问题,如何解决的。

1

import requests
import re

url = "https://www.ebi.ac.uk/chembl/compound_report_card/CHEMBL25/"
req = requests.get(url)
req.encoding = "utf-8"
html = req.text
line1 = re.search('(["a-z:]+[" ]+)([A-Z]+)', html)
if line1 is not None:
    print("英文名称:",line1.group(2))

line2 = re.search('([A-Za-z":]+\s)((\-)?\d+(\.\d{1,2}))', html)
if line2 is not None:
    print("分子量:",line2.group(2))


line3 = re.search('([smile:"]{8}\s\S*\s*\S)([A-Za-z0-9=()]+)', html)
if line3 is not None:
    print("SMILES:",line3.group(2))


line4 = re.search('([image:" ]{10})(((http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?))', html)
if line4 is not None:
    global pic
    img=line4.group(2)
    try:
        pic = requests.get(line4.group(2),timeout=100)
    except requests.exceptions.InvalidURL:
        print("无法下载")

    fileurl = "C:/Users/Polo/Desktop/python 高级编程/实验五下载的图片/"+img.split(r'/')[-1]+".svg"
    fp = open(fileurl, 'wb')  # 文件名
    fp.write(pic.content)
    fp.close()
    print("图片下载完成!")

2

import parsel
from urllib import request
import urllib
url='https://pubmed.ncbi.nlm.nih.gov/?term=cancer'
req = urllib.request.urlopen(url)
req = req.read().decode()
webtext = parsel.Selector(req)
result=webtext.css('.value::text').get().strip(",")
print("与癌症相关论文数量:",result)
title= webtext.css('.full-docsum')
for i in title:
    id=i.css('.docsum-pmid ::text').get()
    print(id,end=' ')
    t=i.css('.docsum-title ::text').getall()
    t=''.join(t).strip()
    print(t)

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值