Home > Back-end >  Using python to read a file and write the content to the end of a URL
Using python to read a file and write the content to the end of a URL

Time:06-03

I'm trying to use python to append a set of stored words to an API url. The URL remains the same throughout, with exception of the stored words. As of right now, the code is:

import requests
import json

with open("words.txt", "r") as f:
    for line in f:
        lines = f.readlines()
        f=line.rstrip("\t")

url = '[url here]' f    
result=requests.get(url)
result.status_code
result.text
result.json()
print (result.json())

The code functions as it should for the first line in the text file, but after that does not continue to scan the text file for the words on lines 2 .

My goal for this project is to have each word go through the process sequentially and print the results given by the API results. I would like to write them to a file as well, but that's another issue I'm going to figure out on my own. This is my first python project so I'm a bit unsure of how to fix this issue, and none of the searching I've done on YouTube is very helpful since this is a rather specific issue as it relates to adding things to urls. I think I'm missing something small, but it's driving me crazy trying to figure out what.

Any help would be extremely appreciated. Thank you!

CodePudding user response:

import requests
import json

with open("words.txt", "r") as f:
    lines = f.readlines()
    for line in lines:
        param = line.rstrip("\t")
        
        url = '[url here]' f    
        result = requests.get(url)
        
        print(result.json())

explanation:

The reason you only returned the request for one line is because the request was made after the loop had finished. f only had one value, and the request was performed on that.

Also, I don't know why you would call readlines() inside the for loop; it should be called prior and is what should be looped over.

Hope this helped :D

CodePudding user response:

As I understood from your question and sample code. You are modifying f in line f=line.rstrip("\t") while looping through f. This is an issue. Another issue is, you don't loop through f, while using readlines(). What you should do is:

with open('words.txt') as f:
    lines = f.readlines()
    for line in f:
        # Inside this for loop, do what you wanted to do with each line after reading it.
        # Example: `url = '[url here]' line` and so on

CodePudding user response:

import requests

url = 'https://example.com'
with open('words.txt', 'r') as f:
    urls = [f'{url}/{line.strip()}' for line in f]
print(urls)

for url in urls:
    res = requests.get(url)
    print(res.json())

CodePudding user response:

import requests

URL = "http://foo.bar"

with open("words.txt", "r") as f:
    lines = f.readlines()
    for line in lines:
        line = line.strip()
        url = f"{URL}/{line}"
        print(url) # you don't need this, but adding this for output as an example
        result = requests.get(url)
        print(result.json())

A few things you can change here to make this more concise. Given that you'll have a constant base URL, define it at the top of the file in all uppercase(abide by PEP-8). This snippet opens the file for reading, collects the lines in the file in an iterable list in lines = f.readlines(). Once collected, you can iterate through this list with the for loop. Performing the action needed on each individual item in the file. F-strings are typically better practice than concatenating with .

If you have a words.txt file containing:

foo
bar
is
great

then the output of the above snippet would be:

python scratch.py 
http://foo.bar/foo
http://foo.bar/bar
http://foo.bar/is
http://foo.bar/great

CodePudding user response:

you only loop through the input, but the requests is outside the loop. So you only request one, all you need todo is add identation begining from url= so the rest of code included in the loop

  • Related