Jump to content

Recommended Posts

Posted

Ok...

Chegoua minha altura de fazer perguntas basicas...

neste código

import urllib2
page = urllib2.urlopen("http://mypage.com/")
print page.read()

porque é que se tem que chamar a função read()?

a variavel page não tem uma string com a pagina que acabei de descarregar?

Para alem disso tenho outra duvida... o função read não está definida na classe urllib2... ou pelo menos nao aparece na sua documentação...

  • 2 weeks later...
Posted

Se fizeres:

print page

o output é: <addinfourl at 35008328L whose fp = <socket._fileobject object at 0x00000000020D76C8>>, ou seja, vais criar um objecto (tipo socket), só quando chamas o método .read é que vais estar efectivamente a fazer a ligação ao servidor e a receber a página.

>>> dir(page)
['__doc__', '__init__', '__iter__', '__module__', '__repr__', 'close', 'code', 'fileno', 'fp', 'geturl', 'headers', 'info', 'msg', 'next', 'read', 'readline', 'readlines', 'url']

<3 life

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.