我试图解析html页面并查找所有图片标签并在django模板中显示
查看
import urllib2
from BeautifulSoup import BeautifulSoup
page = urllib2.urlopen("http://www.flipkart.com/")
soup = BeautifulSoup(page)
tags=soup.findAll('img')
template = get_template('welcome.html')
variables = RequestContext(request,{'tags':tags})
output = template.render(variables)
return HttpResponse(output)
模板
{% block content %}
<div class="row">
<ul class="thumbnails">
{% for row in tags %}
<li >
<span>Flash</span>
<a href="#" class="thumbnail">
{{ row }}
</a>
</li>
{% endfor %}
</ul>
</div>
{% endblock %}
我打印标签
[<img src="http://passets-cdn.pinterest.com/images/search.gif" alt="" />, <img src="http://media-cache2.pinterest.com/upload/422281184577033_NvxwzARh_b.jpg" alt="#wedding #bouquet #flowers" class="PinImageImg" style="height: 288px;" />, <img src="http://media-cache0.pinterest.com/avatars/heygirlfriend-33.jpg" alt="Profile picture of Heather Carpenter" />, <img src="http://media-cache2.pinterest.com/avatars/lilizzy08_1330284092.jpg" class="profile user_image" alt="Profile picture of JoAnn Boyle Barker" />, <img src="http://media-cache2.pinterest.com/avatars/camelotparty_1330114747.jpg" class="profile user_image" alt="Profile picture of Camelot Party" />, <img src="http://media-cache5.pinterest.com/avatars/mamababe13_1327965590.jpg" class="profile user_image" alt="Profile picture of Irene Hardin Sanchez" />, <img src="http://media-cache5.pinterest.com/avatars/mpowers213_1333304368.jpg" class="profile user_image" alt="Profile picture of Maggie Powers" />, <img src="http://media-cache7.pinterest.com/avatars/apricot1026_1334529181.jpg" class="profile user_image" alt="Profile picture of Michelle Nadel" />, <img src="http://media-cache4.pinterest.com/upload/169025792234929326_WtMMM67J_b.jpg" alt="Dr. Oz Metabolism Booster" class="PinImageImg" style="height: 256px;" />, <img src="http://media-cache6.pinterest.com/avatars/karenmigala_1332630951.jpg" alt="Profile picture of Karen Migala" />, <img src="http://media-cache9.pinterest.com/avatars/dutchjohnson-26.jpg" class="profile user_image" alt="Profile picture of Dutch Johnson" />]
当我试图打印标签时,有很多img标签,但我在浏览器中显示一个空列表(显示时)。
HTML page Displays
多个空列表,如[] [] [] [] [] []
Rendered Content
<li >
<span>Flash</span>
<a href="#" class="thumbnail">
[]
</a>
</li>
<li >
<span>Flash</span>
<a href="#" class="thumbnail">
[]
</a>
</li>
<li >
<span>Flash</span>
<a href="#" class="thumbnail">
[]
</a>
</li>
<li >
<span>Flash</span>
<a href="#" class="thumbnail">
[]
</a>
</li>
<li >
<span>Flash</span>
<a href="#" class="thumbnail">
[]
</a>
</li>
请帮忙
答案 0 :(得分:3)
啊,那是因为BeautifulSoup的Tag
是可调用的,因此Django模板直接调用它
class Tag(PageElement):
...
def __call__(self, *args, **kwargs):
"""Calling a tag like a function is the same as calling its
findAll() method. Eg. tag('a') returns a list of all the A tags
found within this tag."""
return apply(self.findAll, args, kwargs)
对于较新版本的Django,您可以将do_not_call_in_templates
设置为True以避免调用。
from BeautifulSoup import Tag
tags=soup.findAll('img')
Tag.do_not_call_in_templates = True
# render