我正在尝试解析 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>
请帮忙