How to print raw html string using urllib3?

[亡魂溺海] 提交于 2020-01-14 14:59:07

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/17257912/how-to-print-raw-html-string-using-urllib3

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!