我一直在浏览Python Requests文档,但我看不到任何我想要实现的功能。
在我的脚本中,我正在设置allow_redirects=True
。
我想知道该页面是否已重定向到其他内容,新网址是什么。
例如,如果起始网址为:www.google.com/redirect
最终网址为www.google.co.uk/redirected
如何获取该网址?
答案 0 :(得分:117)
您正在寻找request history。
response.history
属性是导致最终网址的响应列表,可在response.url
中找到。
response = requests.get(someurl)
if response.history:
print "Request was redirected"
for resp in response.history:
print resp.status_code, resp.url
print "Final destination:"
print response.status_code, response.url
else:
print "Request was not redirected"
演示:
>>> import requests
>>> response = requests.get('http://httpbin.org/redirect/3')
>>> response.history
(<Response [302]>, <Response [302]>, <Response [302]>)
>>> for resp in response.history:
... print resp.status_code, resp.url
...
302 http://httpbin.org/redirect/3
302 http://httpbin.org/redirect/2
302 http://httpbin.org/redirect/1
>>> print response.status_code, response.url
200 http://httpbin.org/get
答案 1 :(得分:40)
这回答了一个稍微不同的问题,但由于我自己也陷入了困境,我希望它对其他人有用。
如果你想使用allow_redirects=False
并直接获取第一个重定向对象,而不是跟随它们的链,并且你只想直接从302响应对象中获取重定向位置,那么{{ 1}}不会工作。相反,它是&#34;位置&#34;头:
r.url
答案 2 :(得分:24)
文档有这个模糊http://docs.python-requests.org/en/latest/user/quickstart/#redirection-and-history
import requests
r = requests.get('http://www.github.com')
r.url
#returns https://www.github.com instead of the http page you asked for
答案 3 :(得分:22)
我认为 requests.head 而非 requests.get 在处理网址重定向时会更安全,请检查github问题here:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.texture_video);
textureView = (TextureView) findViewById(R.id.textureView);
listView = (ListView) findViewById(R.id.list);
.
.
.
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, values);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if (position == 0) {
onDestroy();
FILE_NAME = "test1.mp4";
textureView.setSurfaceTextureListener(VideoAssetActivity.this);
onSurfaceTextureAvailable(textureView.getSurfaceTexture(), view.getWidth(), view.getHeight());
} else if (position == 1) {
onDestroy();
FILE_NAME = "test2.mp4";
textureView.setSurfaceTextureListener(VideoAssetActivity.this);
onSurfaceTextureAvailable(textureView.getSurfaceTexture(), view.getWidth(), view.getHeight());
} else if (position == 2) {
onDestroy();
FILE_NAME = "test3.mp4";
textureView.setSurfaceTextureListener(VideoAssetActivity.this);
onSurfaceTextureAvailable(textureView.getSurfaceTexture(), view.getWidth(), view.getHeight());
} else if (position == 3) {
onDestroy();
FILE_NAME = "test4.mp4";
textureView.setSurfaceTextureListener(VideoAssetActivity.this);
onSurfaceTextureAvailable(textureView.getSurfaceTexture(), view.getWidth(), view.getHeight());
}
}
});
textureView.setSurfaceTextureListener(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mMediaPlayer != null) {
mMediaPlayer.stop();
mMediaPlayer.release();
mMediaPlayer = null;
}
}
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int i, int i2) {
Surface surface = new Surface(surfaceTexture);
try {
AssetFileDescriptor afd = getAssets().openFd(FILE_NAME);
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
mMediaPlayer.setSurface(surface);
mMediaPlayer.setLooping(true);
mMediaPlayer.prepareAsync();
onSurfaceTextureUpdated(surfaceTexture);
mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
// set up media controller
videoController = new MediaController(VideoAssetActivity.this);
videoController.setAnchorView(textureView);
videoController.setPadding(0, 0, 0, 0);
mediaPlayer.start();
}
});
} catch (IllegalArgumentException e) {
Log.d(TAG, e.getMessage());
} catch (SecurityException e) {
Log.d(TAG, e.getMessage());
} catch (IllegalStateException e) {
Log.d(TAG, e.getMessage());
} catch (IOException e) {
Log.d(TAG, e.getMessage());
}
}
答案 4 :(得分:7)
对于python3.5,您可以使用以下代码:
import urllib.request
res = urllib.request.urlopen(starturl)
finalurl = res.geturl()
print(finalurl)