2

Trying to loop through titles of movies in a text file to call API and store the response to a text file.

file contains one title on every line ex.

Titanic
Avatar
A Star Is Born

the api i am trying to use is from www.odmpapi.com

This is what i have to correctly join and create the weblink

import requests
import sys

prefixURL = 'http://www.omdbapi.com/?t='
suffixURL = '&apikey=xxx4s23'

text_file = open("url.txt", "w")

with open('print.txt', 'r') as f:
    for i in f:
        uri = prefixURL + i.rstrip(' \n\t') + suffixURL
        print uri
        text_file.write(url)
        text_file.write('\n')

text_file.close()

text_file = open("responses.txt", "w")


with open('url.txt', 'r') as f2:
    for i in f2:
        url = i.strip(' \n\t')
        batch = requests.get(i.rstrip(' \n\t'))
        data = batch.text
        print data
    text_file.write(data)
    text_file.write('\n')

text_file.close()

This writes to responses.txt only the last title in the list.

8
  • There is no reference to par1 & part2 I think you need to refactor these to prefixURL & suffixURL. Commented Feb 26, 2019 at 23:00
  • @wwii Thanks. used <code></code> Newbie mistake. Commented Feb 26, 2019 at 23:01
  • You have a loop, and looks like you know how to write to a file. What are you having problems with? Are you trying to figure out how to make the request? Have you looked through the urllib.requests docs or the Requests: HTTP for Humans docs? Commented Feb 26, 2019 at 23:07
  • I think you might benefit from looking into context managers in order to avoid leaking file descriptors. Maybe not relevant here, but good for future. Commented Feb 26, 2019 at 23:10
  • @wwii I am doing this but it only writes to responses.txt only the last title in the list ``` text_file = open("responses.txt", "w") #send every uri to the api and write the respsones to a textfile with open('url.txt', 'r') as f2: for i in f2: url = i.strip(' \n\t') batch = requests.get(i.rstrip(' \n\t')) data = batch.text print data text_file.write(data) text_file.write('\n') text_file.close() ``` Commented Feb 26, 2019 at 23:12

1 Answer 1

3

Try the following kind of approach:

import requests
import sys

base_url = 'http://www.omdbapi.com/?t={}&apikey=xxx4s23'

with open('print.txt', 'r') as f_input, open('responses.txt', 'w') as f_output:
    for line in f_input:
        search_term = line.strip(' \n\t')
        url = base_url.format(search_term)
        print url
        batch = requests.get(url)
        f_output.write("{},{}\n".format(url, batch.text))

This writes the URL and the result into the output file. Python's .format() command can be used to put your search time into the base_url without having to split it up and use string concatenation. It works by replacing each {} with a passed argument.

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

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.