python pycurl 获取最终 url 重定向

标签 python pycurl

我需要访问网站 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/

相关文章:

python - 破折号错误地格式化了时间序列的 x 轴

python - 在cygwin中安装python和make

python - pycurl - 302 重定向/页面移动

python - 使用 Selenium + python 和 google chrome 115.x 实现 Web 自动化 >

python - Dash Python - 选择多个参数时制作子图

openssl - Pycurl 不是针对 Openssl 编译的,当我尝试使用 wfuzz 时如何解决这个问题?

python - cURL (pycurl) 通过 HTTP 代理的 FTP

python - Tornado curl http客户端无法获取二进制文件

python - 使用 Python 访问简单的 API

python - Selenium 可以用来突出网页的各个部分吗?