Home > Software engineering >  python ads / when read url from file
python ads / when read url from file

Time:04-20

My first python and stackoverflow attempt.

reading a url from a variable eg

url = 'http://google.com/' 

for using with requests works. But when i load a line using open eg

...  
  with open('001aTOz.txt', 'r') as f:
        for link in f:
            lineCounter  = 1
            if lineCounter == printline:
                url = (f.readline())
                Get_ip()

the line (url) ads some weird text to the end from the line

def Check_ip():
        resp = requests.get(url, allow_redirects=True, timeout=3)
        print(resp.url)
        ...

Output

http://google.com/

Request to http://google.com/
 timed out

so

resp = requests.get(url, allow_redirects=True, timeout=3)

works from a variable in the code but adds when i try to read it from a file

any suggestions.

CodePudding user response:

is an ASCII character for newline, which ends a line in a text file. You have to remove it before your requests, for example with .strip():

with open('001aTOz.txt', 'r') as f:
    for link in f:
        url = link.strip()
        resp = requests.get(url, allow_redirects=True, timeout=3)
        print(resp.url)
  • Related