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
 - 修改列表中的数据帧不起作用 2022-01-01
 - Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
 - PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
 - 在Python中从Azure BLOB存储中读取文件 2022-01-01
 - 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
 - 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
 - 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
 - 求两个直方图的卷积 2022-01-01
 - PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
 
    	
    	
    	
    	
    	
    	
    	
    	
				
				
				
				