简体   繁体   English

Python URL 重定向问题

[英]Python URL Redirect Problem

I've got a link that I know redirects to another end url, and I'm trying to get the address for that end url using python.我有一个我知道重定向到另一端 url 的链接,我正在尝试使用 python 获取该端 url 的地址。 But the original link is a little weird, and doesn't work like a normal redirect, and I can't figure out why.但是原始链接有点奇怪,并且不像正常的重定向那样工作,我不知道为什么。 When I post the link (the link's below for you try, if you'd like) into a browser, it redirects perfectly.当我将链接(如果您愿意,可以在下面尝试)发布到浏览器中时,它会完美地重定向。 But when I run the following code, it doesn't.但是当我运行以下代码时,它没有。

import urllib2
request = urllib2.Request('http://www.facebook.com/ajax/emu/end.php?eid=AQJSWpZ3e4cCTHoNdahpJzPYzmzHOENzbTWBVlW4SgIxX0rL9bo6NXmS3q06cjeh5jO9wbsmr3IyGrpbXPSj0GPLbRJl4VUH-EBnmSy_R4j7iYzpMe1ooZ6IEqSEIlBl0-5SEldIhxI82m75YPa5nOhuBdokiwTw79hoiRB-Zn1auxN-6WLVe3e5WNSt3HLAEjZL-2e4ox_7yAyLcBo1nkamEvShTyZ-GfIf0A9oFXylwRnV8oNaqNmUnqrFYqDbUhzh7d6LSm3jbv1ue2coS3w8N7OxTKVwODHa-Hd3qRbYskB9weio8eKdDFtkvDKuzSSq5hjr711UjlDsgpxLuAmdD95xVwpomxeEsBsMCYJoUEQYa-cM7q3W1aiIYBHlyn2__t74qHWVvzK5zaLKFMKjRFQqphDlUMgMni6AP1VHSn1wli_3lgeVD8TzcJMSlJIF7DC_O44WdjBIMY8OufER3ZB_mm2NqwUe6cvV9oV9SNyYHE4UUURYjW_Z6sUxz3SpHG8c6QxJ-ltSeShvU3mIwAhFE3M0jGTg7AQ7nIoOUfC8PDainFZ1NV8g31aqaqDsF7UxdlOmBT6w-Y8TPmHOXfSlWB-M3MQYUBmcWS3UzlbSsavQG8LXPqYbyKfvkAfncSnZS3_tkoqbTksFirQWlSxJ3mgXrO5PqopH63Esd9ynCbFQM1q_3_wgkYvTeGS9XK6G63_Ag3N9dCHsO_bCJToJT4jeHQCSQ83cb1U5Qpe_7EWbw1ilzgyL-LBVrpH424dwK-4AoaL00W-gWzShSdOynjcoGeB7KE0pHbg-XhuaVribSodriSGybNdADBosnddVvZldY22-_97MqEuA&amp&c=4&amp&f=4&amp&ui=6003071106023-id_4e0b51323f9d01393198225&amp&en=1&amp&a=0&amp&sig=78154')
opener = urllib2.build_opener()
f = opener.open(request)
f.geturl()

I simply get my original url back.我只是把我原来的 url 拿回来了。 I encounter the same problem when I save cookies and use mechanize.当我保存 cookies 并使用机械化时,我遇到了同样的问题。 Any help would be much appreciated!任何帮助将非常感激! Thanks!谢谢!

(original answer deleted) (原答案已删除)

If you look at the contents of f.read() you'll see what's going on here.如果您查看f.read()的内容,您会看到这里发生了什么。 Instead of returning a 301 or 302 that redirects to the new URL, Facebook actually returns a real HTML document - which contains a piece of Javascript that uses document.location.replace to change the URL in the browser. Instead of returning a 301 or 302 that redirects to the new URL, Facebook actually returns a real HTML document - which contains a piece of Javascript that uses document.location.replace to change the URL in the browser.

There's no easy way of replicating that with Python - the best thing to do is to parse the document with something like BeautifulSoup to find the Javascript, and somehow extract the new URL. There's no easy way of replicating that with Python - the best thing to do is to parse the document with something like BeautifulSoup to find the Javascript, and somehow extract the new URL. It won't be pretty.不会很漂亮

It looks like this is using Javascript to perform the redirect.看起来这是使用 Javascript 来执行重定向。 You'll either have to figure out exactly how the Javascript is performing the redirects and pull out the appropriate urls, or you'll have to actually run the Javascript.您要么必须弄清楚 Javascript 是如何执行重定向并提取适当的 url,要么必须实际运行 Javascript。 As far as I know, running Javascript from python is not an easy task.据我所知,从 python 运行 Javascript 并不是一件容易的事。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM