I have a link which redirects to an external website... I want to find out what is the final URL that the link redirects to. I tried:
requests.get("link.which.redirects.and.has.dynamic.js.code.com")
But I could not get the final redirected URL as it is built dynamically... I am not sure exactly what happens but there are some JavaScript code involved in loading of the page and the final result is a redirect to an external page.
So instead I tried Selenium and ChromeDriverManager.
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
class MySpider(scrapy.Spider):
name = 'my_spider'
def __init__(self):
self.driver = webdriver.Chrome(ChromeDriverManager().install())
def parse(self, response):
link = "link.which.redirects.and.has.dynamic.js.code.com"
self.driver.get(link)
time.sleep(1) # without this wait, driver.current_url is not the final redirect
url = self.driver.current_url
The code above loads the entire page, in order to get the redirect URL, is there a more efficient way to get the redirect URL?