質問

i need visit website whit pycurl, follow redirects, and print final url, i write this python code :

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()

i need print the final url, how can i make this ? thanks.

the solution is this : url_effective = c.getinfo(c.EFFECTIVE_URL)

役に立ちましたか?

解決

here's an adaptation of the PHP script I linked in the comments:

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

though, as this example shows, you may not always have the full URI, only the path part of the URI (but if that's the case, that's easy to add the fqdn back)

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top