通过请求获取重定向的URL

To get redirected URL with requests(通过请求获取重定向的URL)

本文介绍了通过请求获取重定向的URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过请求获得红色ı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

这篇关于通过请求获取重定向的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:通过请求获取重定向的URL

基础教程推荐