在 Python 3.4 中使用 urllib 获取用于打开 URL 的输入

Taking Inputs for Opening URLs with urllib in Python 3.4

假设我有一个 ID 列表(让我们初始化我们的代码)

import urllib.request 
import urllib.parse 
import re  
from urllib import request 

test_list = ['1234','2345','34566','55564','19435']

我的目标是现在使用 API 调用和 urllib 遍历此测试列表,但我不确定使用 urllib.request.urlopen('www.api.com/')[=13 的语法=]

for i in test_list:
  #Now I want to iterate through each ID and make an individual API call to pubmed with each one.
  #Obviously I will nest other operations within the for loop, however, I cannot get the syntax for
  id_request = urllib.request.urlopen('http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=pubmed&id={i}'.format(i=i))

显示的代码运行良好,

for i in test_list:
  id_request = urllib.request.urlopen('http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=pubmed&id={i}'.format(i=i))
  xml = id_request.read()
  print(xml)

你得到,例如:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE eSummaryResult PUBLIC "-//NLM//DTD esummary v1 20041029//EN" "http://eutils.ncbi.nlm.nih.gov/eutils/dtd/20041029/esummary-v1.dtd">
<eSummaryResult>
<DocSum>
<Id>19435</Id>
<Item Name="PubDate" Type="Date">1977 May</Item>
<Item Name="EPubDate" Type="Date"></Item>
<Item Name="Source" Type="String">J Biochem</Item>
<Item Name="AuthorList" Type="List">
<Item Name="Author" Type="String">Nishina Y</Item>
<Item Name="Author" Type="String">Horiike K</Item>
<Item Name="Author" Type="String">Shiga K</Item>
<Item Name="Author" Type="String">Miyake Y</Item>
<Item Name="Author" Type="String">Yamano T</Item>
</Item>
<Item Name="LastAuthor" Type="String">Yamano T</Item>
<Item Name="Title" Type="String">Effect of halide anions on the binding of FAD to D-amino acid oxidase and the tryptophanyl fluorescence of the apoenzyme.</Item>
<Item Name="Volume" Type="String">81</Item>
<Item Name="Issue" Type="String">5</Item>
<Item Name="Pages" Type="String">1455-63</Item>
<Item Name="LangList" Type="List">
<Item Name="Lang" Type="String">English</Item>
</Item>
<Item Name="NlmUniqueID" Type="String">0376600</Item>
<Item Name="ISSN" Type="String">0021-924X</Item>
<Item Name="ESSN" Type="String">1756-2651</Item>
<Item Name="PubTypeList" Type="List">
<Item Name="PubType" Type="String">Journal Article</Item>
</Item>
<Item Name="RecordStatus" Type="String">PubMed - indexed for MEDLINE</Item>
<Item Name="PubStatus" Type="String">ppublish</Item>
<Item Name="ArticleIds" Type="List">
<Item Name="pubmed" Type="String">19435</Item>
<Item Name="eid" Type="String">19435</Item>
<Item Name="rid" Type="String">19435</Item>
</Item>
<Item Name="History" Type="List">
<Item Name="pubmed" Type="Date">1977/05/01 00:00</Item>
<Item Name="medline" Type="Date">1977/05/01 00:01</Item>
<Item Name="entrez" Type="Date">1977/05/01 00:00</Item>
</Item>
<Item Name="References" Type="List"></Item>
<Item Name="HasAbstract" Type="Integer">1</Item>
<Item Name="PmcRefCount" Type="Integer">1</Item>
<Item Name="FullJournalName" Type="String">Journal of biochemistry</Item>
<Item Name="ELocationID" Type="String"></Item>
<Item Name="SO" Type="String">1977 May;81(5):1455-63</Item>
</DocSum>

</eSummaryResult>

奖金

使用 biopython

from Bio import Entrez
Entrez.email = 'email@example.com'
handle = Entrez.efetch(db="pubmed", id='19435', rettype="medline", retmode="text")
handle.read()

你得到:

PMID- 19435
OWN - NLM
STAT- MEDLINE
DA  - 19771020
DCOM- 19771020
LR  - 20131121
IS  - 0021-924X (Print)
IS  - 0021-924X (Linking)
VI  - 81
IP  - 5
DP  - 1977 May
TI  - Effect of halide anions on the binding of FAD to D-amino acid oxidase and the
      tryptophanyl fluorescence of the apoenzyme.
PG  - 1455-63
AB  - The quenching of tryptophanyl fluorescence of native and denatured D-amino acid
      oxidase from hog kidney was measured. About 60% of the tryptophanyl fluorescence 
      of the native apoenzyme was quenched by iodide at pH 8.3, and 25 degrees C. All
      of the tryptophanyl fluorescence of the apoenzyme in 6 M guanidine hydrochloride 
      was quenched. The tryptophanyl fluorescence quenching of the holoenzyme by
      1-methyl nicotinamide chloride was low in comparison with that of the apoenzyme. 
      These results of the quenching experiments are discussed based on the
      intermolecular collision quenching mechanism. By measuring the fluorescence
      intensities of the tryptophanyl residues and FAD of the holoenzyme solution, and 
      the fluorescence polarization of the holoenzyme solution containing halide anions
      such as iodide, bromide, chloride, or fluoride, we found that FAD dissociates
      from the holoenzyme in the presence of iodide, bromide, or chloride, and the
      ability to dissociate FAD from the holoenzyme decreases in order iodide, bromide,
      and chloride. However, fluoride seems to enhance the association reaction of FAD 
      with the apoenzyme. These results were consistent with the visible absorption
      spectra and derivative spectra of free FAD and the holoenzyme in the presence and
      absence of halide anions.
FAU - Nishina, Y
AU  - Nishina Y
FAU - Horiike, K
AU  - Horiike K
FAU - Shiga, K
AU  - Shiga K
FAU - Miyake, Y
AU  - Miyake Y
FAU - Yamano, T
AU  - Yamano T
LA  - eng
PT  - Journal Article
PL  - JAPAN
TA  - J Biochem
JT  - Journal of biochemistry
JID - 0376600
RN  - 0 (Apoenzymes)
RN  - 0 (Bromides)
RN  - 0 (Chlorides)
RN  - 0 (Iodides)
RN  - 146-14-5 (Flavin-Adenine Dinucleotide)
RN  - 8DUH1N11BX (Tryptophan)
RN  - EC 1.4.3.3 (D-Amino-Acid Oxidase)
RN  - Q80VPU408O (Fluorides)
SB  - IM
MH  - Animals
MH  - Apoenzymes/metabolism
MH  - Binding Sites
MH  - *Bromides/pharmacology
MH  - *Chlorides/pharmacology
MH  - *D-Amino-Acid Oxidase/metabolism
MH  - *Flavin-Adenine Dinucleotide
MH  - *Fluorides/pharmacology
MH  - *Iodides/pharmacology
MH  - Kidney/enzymology
MH  - Kinetics
MH  - Osmolar Concentration
MH  - Protein Binding
MH  - Protein Conformation
MH  - Spectrometry, Fluorescence
MH  - Spectrophotometry
MH  - Swine
MH  - Tryptophan/analysis
EDAT- 1977/05/01
MHDA- 1977/05/01 00:01
CRDT- 1977/05/01 00:00
PST - ppublish
SO  - J Biochem. 1977 May;81(5):1455-63.
xmls = [urllib.request.urlopen('http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=pubmed&id='+x).read() for x in test_list]


print (xmls[0])
print (xmls[1])
.. etc