0

我需要 RSS 文件的描述标签内的 URL。我正在尝试解析以下链接中的图像。“ibnlive.in.com/ibnrss/rss/shows/worldview.xml”我需要图片链接。我正在使用 urllib 和美丽的汤来解析细节。我正在尝试解析项目标签内的标题、描述、链接和图像。我可以解析标题、描述和链接。但我无法解析描述标签内的图像。

XML:

<item>
    <title>World View: US shutdown ends, is the relief only temporary?</title>
    <link>http://ibnlive.in.com/videos/429157/world-view-us-shutdown-ends-is-the-relief-only-temporary.html</link>
    <description>&lt;img src='http://static.ibnlive.in.com/ibnlive/pix/sitepix/10_2013/worldview_1810a_90x62.jpg' width='90' height='62'&gt;The US Senate overwhelmingly approved a deal on Wednesday to end a political crisis that partially shut down the federal government and brought the world's biggest economy to the edge of a debt default that could have threatened financial calamity.</description>
    <pubDate>Fri, 18 Oct 2013 09:34:32 +0530</pubDate>
    <guid>http://ibnlive.in.com/videos/429157/world-view-us-shutdown-ends-is-the-relief-only-temporary.html</guid>
    <copyright>IBNLive</copyright>
    <language>en-us</language>
</item>

视图.py

from django.conf import settings
from django.shortcuts import render
from django.http import HttpResponse
from django.utils.html import strip_tags
from os.path import basename, splitext
import os
import urllib
from bs4 import BeautifulSoup

def international(request):
    arr=[]
    #asianage,oneinindia-papers
    a=["http://news.oneindia.in/rss/news-international-fb.xml","http://www.asianage.com/rss/37"]
    for i in a:
        source_txt=urllib.urlopen(i)
        b=BeautifulSoup(source_txt.read())
        for q in b.findAll('item'):
            d={}
            d['desc']=strip_tags(q.description.string).strip('&nbsp')
            if q.guid:
                d['link']=q.guid.string
            else:   
                d['link']=strip_tags(q.comments)
            d['title']=q.title.string
            for r in q.findAll('description'):
                d['image']=r['src']
            arr.append(d)
    return render(request,'feedpars.html',{'arr':arr})  

HTML

<html>
    <head></head>
    <body>
        {% for i in arr %}
             <p>{{i.title}}</p>
             <p>{{i.desc}}</p>
             <p>{{i.guid}}</p>
             <img src="{{i.image}}" style="width:100px;height:100px;"><hr>
        {% endfor %}
    </body>
</html>

我的输出中没有显示任何内容。

4

1 回答 1

0

1/ 正如我在这里已经告诉过你的,如何在解析时获取 xml 文件的描述标签中的图像 url?:这是一个 rss 提要,因此请使用适当的工具:https ://pypi.python.org/pypi/feedparser

2/ 描述中没有适当的“img”标签,html 标记已被实体编码。要获取 url,您必须解码描述的内容(以获取标签)并将生成的 html 片段传递给您的 HTML 解析器,或者 - 因为它可能不像完整的 html 文档那样复杂 - 只需使用普通的编码内容的正则表达式。

于 2013-10-25T06:56:46.630 回答