私を助けてください..Pythonが初めてです。Webページのタイトル、画像、説明を取得しようとしました。タイトルと画像は取得できますが、説明は取得できません。説明を取得すると、html タグと一緒に表示されます。私はこのURLを取ります。 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})
最初の関数は、画像とタイトルを取得してそれぞれ表示することです。2 番目の関数は、説明を取得することです。
私は両方の機能に対して個別のテンプレートを作成しました。最初の関数のテンプレート:
<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>
最初の関数では、出力が正しく取得されています。しかし、説明だけのために、次のようにこの出力を取得しています(1回の反復だけで出力を入れます)
<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.”</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.”</p>