[英]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.