我需要访问网站 whit pycurl,跟随重定向,并打印最终 url,我写了这个 python 代码:
c = pycurl.Curl()
c.setopt(c.URL, 'http://localhost/redirect.php')
c.setopt(c.HTTPPOST, values)
c.setopt(c.WRITEFUNCTION, buf_pagina.write)
c.setopt(c.HEADERFUNCTION, buf_header.write)
c.setopt(c.CONNECTTIMEOUT, 30)
c.setopt(c.AUTOREFERER,1)
c.setopt(c.FOLLOWLOCATION, 1)
c.setopt(c.COOKIEFILE, '')
c.setopt(c.TIMEOUT, 30)
c.setopt(c.USERAGENT, '')
c.perform()
我需要打印最终网址,我该怎么做?谢谢。
解决方案是这样的:url_effective = c.getinfo(c.EFFECTIVE_URL)
最佳答案
这是我在评论中链接的 PHP 脚本的改编版:
import pycurl
import sys
import StringIO
o = StringIO.StringIO()
h = StringIO.StringIO()
c = pycurl.Curl()
c.setopt(c.URL, 'http://stackoverflow.com/questions/21444891')
# c.setopt(c.HTTPPOST, values)
c.setopt(c.WRITEFUNCTION, o.write)
c.setopt(c.HEADERFUNCTION, h.write)
c.setopt(c.CONNECTTIMEOUT, 30)
c.setopt(c.AUTOREFERER,1)
c.setopt(c.FOLLOWLOCATION, 1)
c.setopt(c.COOKIEFILE, '')
c.setopt(c.TIMEOUT, 30)
c.setopt(c.USERAGENT, '')
c.perform()
h.seek(0)
location = ""
for l in h:
if "Location" in l:
location = l.split(": ")[-1]
print location
不过,如本示例所示,您可能并不总是拥有完整的 URI,只有 URI 的路径部分(但如果是这种情况,则很容易将 fqdn 添加回来)
关于python pycurl 获取最终 url 重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21444891/