0
import requests

def extractlink():
    with open('extractlink.txt', 'r') as g:
        print("opened extractlink.txt for reading")
        contents = g.read()
        headers = {'userAgent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36'}
        r = requests.get(contents, headers=headers)
        print(("Links to " + r.url))
        time.sleep (2)

Currently, r.url is just linking to the url found in 'extractlink.txt'

I'm looking to fix this script to find the final redirected url and print the result. It appears the issue lies somewhere in the request for the URL, despite trying many alternatives and troubleshooting steps, my issue doesn't seem to be solved like the rest.

When debugging, r.history reads as [] and r.status_code reads as 403 even though the link redirects as a 302 in browser. Any ideas?

(extractlink.txt is just a one line file with a link to a redirect/link shortener)

Dann
  • 159
  • 6
  • Your code worked fine when I tried it, so it's unclear what issue there is... how are you putting the `url` in your `extractlink.txt` file? (it should be like `https://example.com`) – l'L'l Dec 18 '18 at 02:55
  • avoid asking the same question in several post – eyllanesc Dec 18 '18 at 02:56
  • Code returns a 403 for me @l'L'l link is a `http://xxxx` The website works fine on wheregoes and browser. – Dann Dec 18 '18 at 03:02

1 Answers1

0

It is difficult to tell without your data, but you may have a line ending in your text file that is changing the URL. You might change read() to:

contents = g.readline().strip()

You might also try executing the request using a Session object:

with requests.Session() as session:
    r = session.get(contents, headers=headers)
Wes Doyle
  • 2,199
  • 3
  • 17
  • 32
  • Still returns a 403, `contents` returns the proper url, the issue lies elsewhere. – Dann Dec 18 '18 at 03:05
  • I've also tried executing the request using `session`, I've done plenty of research before making my post but haven't found a solution. – Dann Dec 18 '18 at 03:12