Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print raw html string using urllib3?

Tags:

python

urllib3

I use below statment to get html string:

import urllib3

url ='http://urllib3.readthedocs.org/'
http_pool = urllib3.connection_from_url(url)
r = http_pool.urlopen('GET',url)

print (r.data)

But the output is :

b'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "b'\n<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"\n  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\n\n\n<html xmlns="http://www.w3.org/1999/xhtml">\n  <head>\n    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />\n    \n\n   .......................................\n</script>\n\n\n\n  </body>\n</html>''

How can I get a raw html string?

like image 782
Mithril Avatar asked Jun 23 '13 05:06

Mithril


People also ask

What is urllib3 PoolManager ()?

The PoolManager class automatically handles creating ConnectionPool instances for each host as needed. By default, it will keep a maximum of 10 ConnectionPool instances. If you're making requests to many different hosts it might improve performance to increase this number: >>> import urllib3 >>> http = urllib3.

How do I use urllib3 in Python?

Python urllib3 send JSON The example sends JSON data. We encode the JSON data into binary format. We specify the Content-Type header in the request. We decode the returned data back to text and print it to the console.


1 Answers

The anwser is print (r.data.decode('utf-8'))
But this statment will break in sublime text 2.
Because of this issue.
When I use IDLE,the output will be fine.

like image 117
Mithril Avatar answered Oct 15 '22 10:10

Mithril