Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

urllib2.urlopen(): getting the size of the content

Tags:

python

urllib2

Still working my way round python whenever work permits it...

I'm querying a load of internal webUI's using a script that uses urllib2.urlopen. I'm wondering how it is possible to get the size of the page content from each request. I can't seem to figure this one out.

Thanks in advance,

MHibbin

like image 706
MHibbin Avatar asked Sep 07 '12 11:09

MHibbin


People also ask

What does Urllib Urlopen return?

The problem here is that urlopen returns a reference to a file object from which you should retrieve HTML. Please note that urllib. urlopen function is marked as deprecated since python 2.6. It's recommended to use urllib2.

What does Urlopen does in Python?

Urllib package is the URL handling module for python. It is used to fetch URLs (Uniform Resource Locators). It uses the urlopen function and is able to fetch URLs using a variety of different protocols.

What does Urllib request Urlopen do?

request is a Python module for fetching URLs (Uniform Resource Locators). It offers a very simple interface, in the form of the urlopen function. This is capable of fetching URLs using a variety of different protocols.

What is urllib2 in Python?

urllib2 is a Python module that can be used for fetching URLs. It defines functions and classes to help with URL actions (basic and digest. authentication, redirections, cookies, etc) The magic starts with importing the urllib2 module.


1 Answers

print len(urlopen(url).read())

or

>>> result = urllib2.urlopen('http://www.spiegel.de')
>>> result.headers['content-length']
'181291'
like image 169
Andreas Jung Avatar answered Sep 28 '22 01:09

Andreas Jung