1

请帮帮我。我是 Python 新手。我试图获取网页的标题、图像和描述。我可以获取标题和图片,但不能获取描述。当我获取描述时,它带有 html 标签。我拿这个网址。 http://www.asianage.com/rss/37 我试过下面的代码views.py

from django.shortcuts import render
from os.path import basename, splitext
import os
import urllib
from bs4 import BeautifulSoup
def parser(request):
    source_txt=urllib.urlopen("http://www.asianage.com/rss/37")
    b=BeautifulSoup(source_txt.read())
    d={}
    for q in b.findAll('item'):
        arr1=[]
        for i in q.findAll('enclosure'):
            arr1.append(i['url'])
        d[q.title.string]=arr1
    return render(request,'link.html',{'arr':d})


def descrip(request):
    source_txt=urllib.urlopen("http://www.asianage.com/rss/37")
    b=BeautifulSoup(source_txt.read())
    arr1=[]
    for q in b.findAll('item'):
        for a in q.findAll('description'):
            arr1.append(a.string)
    return render(request,'desc.html',{'arr':arr1})

第一个功能是获取图像和标题并分别显示。第二个功能是获取描述。

我为这两个函数编写了单独的模板。第一个功能的模板:

<html>
<head>
</head>
<body>
<div style="width:760px;margin:auto;border:1px solid #DCDCDC;">
{% for i,j,k in arr.items %}
<div style="width:250px;height:250px;float:left;border:1px solid #DCDCDC;">
    <div style="width:240px;height:180px;border:1px solid #DCDCDC;margin:auto;">
    <img src="{{j.0}}" style="width:240px;height:180px;"></div>
    <div style="width:240px;height:60px;border:1px solid #DCDCDC;margin:auto"><a href="/descrip">{{i}}</a></div>
</div>
{{k}}
{% endfor %}
</div>
</body>
</html>

访问描述的模板。

<html>
<head>
</head>
<body>
{% for m in arr %}
    <p>{{m}}</p><hr>
{% endfor %}
</body>
</html>

对于第一个功能,我得到了正确的输出。但仅用于描述,我得到的输出如下(我仅将输出用于一次迭代)

<div class="all-attached-images"><div style="width: 430px" class="image-attach-body"><a href="/i3jpg-603"><img src="http://www.asianage.com/sites/default/files/images/I3_6.jpg" alt="I3.jpg" title="I3.jpg" class="image image-content_image " width="430" height="317" /></a></div> </div><p>Malala Yousafzai hit back at claims that she has become a figure of the West, insisting she was proud to be a Pakistani.<br /> <!--break--><br /> The 16-year-old, who was shot by the Taliban for championing girls’ right to an education, claimed she retained the support of people in her homeland, and reiterated her desire to enter Pakistani politics.</p> <p>The activist was shot in the head on her school bus on October 9 last year for speaking out against the Taliban.</p> <p>She was flown for specialist care in Britain, where she has continued her education, while she has been feted and honoured in the West.</p> <p>On Thursday, she won the European Union’s prestigious Sakharov human rights prize, while US President Barack Obama welcomed her to the White House on Friday.</p> <p>Asked in a BBC television interview broadcast Sunday about some people in Pakistan thinking she was a “figure of the West” and “a Westerner now”, she said: “My father says that education is neither Eastern or Western. Education is education: it’s the right of everyone.</p> <p>“The thing is that the people of Pakistan have supported me. They don’t think of me as Western. I am a daughter of Pakistan and I am proud that I am a Pakistani.</p> <p>“On the day when I was shot, and on the next day, people raised the banners of ‘I am Malala’. They did not say ‘I am Taliban’.</p> <p>“They support me and they are encouraging me to move forward and to continue my campaign for girls’ education.”&lt;/p> <p>She highlighted the problem of education in the midst of the Syrian conflict.</p> <p>“We want to help every child in every country that we can,” she said.</p> <p>“We will start from Pakistan and Afghanistan and Syria now, especially because they are suffering the most and they are on the top that need our help.</p> <p>“Later on in my life I want to do politics and I want to become a leader and to bring the change in Pakistan.</p> <p>“I want to be a politician in Pakistan because I don’t want to be a politician in a country which is already developed.”&lt;/p> 
4

0 回答 0