渲染到模板时,不显示urllib.urlopen的输出

时间:2011-09-22 09:37:45

标签: python django-templates django-views

我正在使用Python 2.7 - 如何将urllib.urlopen的输出转换为字符串?

data =  urllib.urlopen('http://www.google.com').read()
test = "testone"
return render_to_response('home.html', locals())

home.html的:

{{data}}
<br/>
{{test}}

浏览器中只显示“testone”。

3 个答案:

答案 0 :(得分:1)

在我的系统上,它已经是一个字符串,因此不需要转换:

In [4]: type(urllib.urlopen('http://www.google.com').read())
Out[4]: <type 'str'>

答案 1 :(得分:1)

>>> import urllib
>>> data = urllib.urlopen('http://www.google.com').read()
>>> type(data)
<type 'str'>

它已经是一个字符串了。

答案 2 :(得分:1)

当我更改网址www.example.org.Its有效。