通过请求获取重定向的URL

2022-02-22 00:00:00 python python-requests url redirect

问题描述

我想通过请求获得红色ıRECTED URL。我的URL是https://twitter.com/i/user/2274951674。当我在浏览器中输入此URL时,该URL重定向https://twitter.com/ozanbayram01

r=requests.get("https://twitter.com/i/user/2274951674")`

r.url #doesnt work

解决方案

我没有任何运气通过使用前面帖子中提到的请求将URL重定向回来。但是我能够使用WebBrowser库解决这个问题,然后使用sqlite3获取浏览器历史记录,并能够获得您想要的结果。

如果找到其他解决方案,请通知我。

以下是我的代码:

import webbrowser
import sqlite3
import pandas as pd
import shutil

webbrowser.open("https://twitter.com/i/user/2274951674")
#source file is where the history of your webbroser is saved, I was using chrome, but it should be the same process if you are using different browser
source_file = 'C:\Users\{your_user_id}\AppData\Local\Google\Chrome\User Data\Default\History'
# could not directly connect to history file as it was locked and had to make a copy of it in different location
destination_file = 'C:\Users\{user}\Downloads\History'
time.sleep(30) # there is some delay to update the history file, so 30 sec wait give it enough time to make sure your last url get logged
shutil.copy(source_file,destination_file) # copying the file.
con = sqlite3.connect('C:\Users\{user}\Downloads\History')#connecting to browser history
cursor = con.execute("SELECT * FROM urls")
names = [description[0] for description in cursor.description]
urls = cursor.fetchall()
con.close()
df_history = pd.DataFrame(urls,columns=names)
last_url = df_history.loc[len(df_history)-1,'url']
print(last_url)

>>https://twitter.com/ozanbayram01

相关文章