【问题标题】:Python: urllib.request.urlretrieve saves an empty file. Writes in it "Supplied id parameter is empty."Python:urllib.request.urlretrieve 保存一个空文件。在其中写入“提供的 id 参数为空”。
【发布时间】:2015-01-25 21:30:41
【问题描述】:

这是我要下载并保存的文件类型:

http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=nuccore&id=562868704,585641505&rettype=fasta&retmode=text

...这是测试代码:

import urllib.request
import xml.etree.ElementTree as ET
mystring = ' '
link = urllib.request.urlopen('http://eutils.ncbi.nlm.nih.gov/entrez/eutils/elink.fcgi?dbfrom=protein&db=nuccore&linkname=protein_nuccore_mrna&id=13591999,149050462')
tree = ET.parse(link)
root = tree.getroot()
for branch in root.iter('Link'):
    for something in branch.iter('Id'):
        mystring += something.text + ','
mRNA = urllib.request.urlretrieve('http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=nuccore&id=' + mystring + '&rettype=fasta&retmode=text', 'C:/Users/User/Documents/mRNA.fasta')

它会创建文件但随后为空,我不知道问题出在哪里。谢谢你的帮助。

【问题讨论】:

    标签: python url download urllib


    【解决方案1】:

    您生成的 URL 包含一个空格;你自己把它放在那里:

    mystring = ' '
    

    如果我用空字符串替换它,您的代码似乎可以工作:

    mystring = ''
    

    您可以使用列表并使用str.join() method 来构建您的值,而不是使用字符串连接:

    elements = []
    for element in root.findall('.//Link/Id'):
        elements.append(element.text)
    mystring = ','.join(elements)
    

    我使用 Element.findall() methodXPath expression 列出所有匹配的 Id 节点。

    【讨论】:

    • 我简直不敢相信...谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-04
    • 2015-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多