【发布时间】:2012-12-18 15:43:01
【问题描述】:
我正在使用 urllib2 模块来读取一个 html 页面,下面是我的代码
code.py
import urllib2, httplib
httplib.HTTPConnection.debuglevel = 1
request = urllib2.Request("http://www.vodafone.in/Pages/tuesdayoffers_che.aspx")
opener = urllib2.build_opener()
f = opener.open(request)
print f.url
结果
'http://www.vodafone.in/pages/tuesdayoffers_che.aspx?cid=che'
当我在浏览器中给出上面的 url 时,它被重定向到 http://www.vodafone.in/pages/home_che.aspx?cid=che,但是从上面的代码中我得到了相同的给定 url
最后如何使用 urrlib2 捕获重定向的 url 并从中读取数据,因为
我有许多 url 将被重定向到其他一些 url,最后我的意图是捕获重定向的 url 并从捕获的 url 中读取数据,那么如何在 python 中使用urllib2 and httplib
【问题讨论】:
-
我试过了,但对我不起作用,所以接近了,你能根据上述情况提供代码吗?
-
我猜该页面使用基于 javascript 的重定向。
标签: python url redirect urllib2 httplib