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
基础教程推荐
猜你喜欢
- 对多索引数据帧的列进行排序 2022-01-01
- Python,确定字符串是否应转换为 Int 或 Float 2022-01-01
- matplotlib 设置 yaxis 标签大小 2022-01-01
- 究竟什么是“容器"?在蟒蛇?(以及所有的 python 容器类型是什么?) 2022-01-01
- 在 Python 中将货币解析为数字 2022-01-01
- kivy 应用程序中的一个简单网页作为小部件 2022-01-01
- Python 中是否有任何支持将长字符串转储为块文字或折叠块的 yaml 库? 2022-01-01
- Kivy 使用 opencv.调整图像大小 2022-01-01
- 在 Django Admin 中使用内联 OneToOneField 2022-01-01
- 比较两个文本文件以找出差异并将它们输出到新的文本文件 2022-01-01
