0

Please correct me if I am wrong as I am a beginner in python.

I have a web services URL which contains an XML file:

http://abc.tch.xyz.edu:000/patientlabtests/id/1345

I have a list of values and I want to append each value in that list to the URL and download file for each value and the name of the downloaded file should be the same to the value appended from the list.

It is possible to download one file at a time but I have 1000's of values in the list and I was trying to write a function with a for loop and I am stuck.

x = [ 1345, 7890, 4729]
for i in x :
     url = http://abc.tch.xyz.edu:000/patientlabresults/id/{}.format(i)
     response = requests.get(url2)

      ****** Missing part of the code ********

        with open('.xml', 'wb') as file:
        file.write(response.content)
        file.close()

The files downloaded from URL should be like

"1345patientlabresults.xml"
"7890patientlabresults.xml"
"4729patientlabresults.xml"

I know there is a part of the code which is missing and I am unable to fill in that missing part. I would really appreciate if anyone can help me with this.

2 Answers 2

1

Accessing your web service url seem not to be working. Check this.

import requests


x = [ 1345, 7890, 4729]
for i in x :
    url2 = "http://abc.tch.xyz.edu:000/patientlabresults/id/"
    response = requests.get(url2+str(i)) # i must be converted to a string

Note: When you use 'with' to open a file, you do not have close the file since it will closed automatically.

with open(filename, mode) as file:
    file.write(data)

Since the Url you provide is not working, I am going to use a different url. And I hope you get the idea and how to write to a file using the custom name

import requests

categories = ['fruit', 'car', 'dog']

for category in categories :
    url = "https://icanhazdadjoke.com/search?term="
    response = requests.get(url + category)
    file_name = category + "_JOKES_2018" #Files will be saved as fruit_JOKES_2018

    r = requests.get(url + category)
    data = r.status_code #Storing the status code in 'data' variable

    with open(file_name+".txt", 'w+') as f:
        f.write(str(data)) # Writing the status code of each url in the file

After running this code, the status codes will be written in each of the files. And the file will also be named as follows:

  • car_JOKES_2018.txt
  • dog_JOKES_2018.txt
  • fruit_JOKES_2018.txt

I hope this gives you an understanding of how to name the files and write into the files.

Sign up to request clarification or add additional context in comments.

Comments

1

I think you just want to create a path using str.format as you (almost) are for the URL. maybe something like the following

import os.path
x = [ 1345, 7890, 4729]
for i in x:
     path = '1345patientlabresults.xml'.format(i)
     # ignore this file if we've already got it
     if os.path.exists(path):
       continue

     # try and get the file, throwing an exception on failure
     url = 'http://abc.tch.xyz.edu:000/patientlabresults/id/{}'.format(i)
     res = requests.get(url)
     res.raise_for_status()

     # write the successful file out
     with open(path, 'w') as fd:
       fd.write(res.content)

I've added some error handling and better behaviour on retry

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.