简体   繁体   English

Python:urllib.request.urlretrieve保存一个空文件。 在其中写道“提供的id参数为空。”

[英]Python: urllib.request.urlretrieve saves an empty file. Writes in it “Supplied id parameter is empty.”

This is the type of file I want to download and save: 这是我要下载并保存的文件类型:

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

...and this is the test code: ...这是测试代码:

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')

It creates the file but is then empty, i have no clue what the problem is. 它创建了文件,但随后为空,我不知道问题是什么。 Thank you for the help. 感谢您的帮助。

Your resulting URL contains a space; 生成的URL包含一个空格; you put it there yourself with: 您可以自己将其放置在以下位置:

mystring = ' '

If I replace that with an empty string instead your code appears to work: 如果我将其替换为空字符串,则您的代码似乎可以正常工作:

mystring = ''

Rather than use string concatenation, you can use a list and use the str.join() method to build your value: 您可以使用列表并使用str.join()方法来构建值,而不是使用字符串连接:

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

I used the Element.findall() method with an XPath expression to list all matching Id nodes. 我将Element.findall()方法XPath表达式一起使用,以列出所有匹配的Id节点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在python 2.7中使用urllib.request.urlretrieve - How can I use urllib.request.urlretrieve with python 2.7 无法在Python中使用“ urllib.request.urlretrieve”下载图像 - failing at downloading an image with “urllib.request.urlretrieve” in Python 单元测试模拟 urllib.request.urlretrieve() Python 3 和内部函数 - Unit test mock urllib.request.urlretrieve() Python 3 and internal function 尝试在Python中下载jpeg时出现urllib.request.urlretrieve错误 - urllib.request.urlretrieve ERROR trying to download jpeg in Python urllib.request.urlretrieve返回损坏的文件(如何处理这种网址?) - urllib.request.urlretrieve returns corrupt file (How to handle this kind of url?) Python,基本问题:如何使用 urllib.request.urlretrieve 下载多个 url - Python, basic question: How do I download multiple url's with urllib.request.urlretrieve 使用urllib.request.urlretrieve下载需要固定的时间 - downloading with urllib.request.urlretrieve takes fixed time HTTP 错误 404:未找到 urllib.request.urlretrieve - HTTP Error 404: Not Found urllib.request.urlretrieve 使用什么命令代替 urllib.request.urlretrieve? - What command to use instead of urllib.request.urlretrieve? urllib.request.urlretrieve不通过HTTPS下载文件 - urllib.request.urlretrieve not downloading files over HTTPS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM