AnsweredAssumed Answered

pass a list into requests.get()

Question asked by jpilbeam Champion on Apr 11, 2020
Latest reply on Apr 13, 2020 by jpilbeam

This gets one URL and prints it in XML format.

import requests
from bs4 import BeautifulSoup

url = r'''
          <AddressValidateRequest USERID="564WILLC0589"><Address><Address1>
          2451 Avalon Ct</Address1><Address2></Address2><City>Aurora</City>

#get the webpage
response = requests.get(url)
#see if the URL has been correctly encoded
r_url = response.text

#parse the downloaded page to get a beautifulsoup object
new_xml = BeautifulSoup(r_url, features = "xml").prettify()
<?xml version="1.0" encoding="utf-8"?>
   2451 AVALON CT


But, I have a list of URLs that need to be printed in XML format. Using this list how can I pass one item at a time into requests.get()? Link to text file.

#text file that has all the URLs
txtfile = r'C:\Users\jpilbeam\USPSAPIWCHDUpdateAll.txt'

#convert text file into a list
with open (txtfile) as f:
    x = (list(map(str.strip ,f.readlines())))