我能够解析rss feed并成功地将链接,磁贴和描述保存到我的数据库,但是当我尝试对同一个Feed中的图像执行相同操作时,我收到错误。我已经检查过xml文件是否有类别' image'与via合作:
d.feed.image {&#39;副标题&#39;:你&#39; SABC新闻&#39;,&#39;链接&#39;:[{&#39; href&#39;:你&#39; http://www.sabc.co.za/news/& #39;,&#39;输入&#39;:你&#39; text / html&#39;,&#39; rel&#39;:u&#39; alternate&#39;}],&#39; title&# 39;:你&#39; SABC新闻&#39;,&#39;身高&#39;:173,&#39;宽度&#39;:308,&#39; title_detail&#39;:{&#39; base& #39;:你&#39; http://www.sabc.co.za/SABC/RSS/news/TopStoryRSSFeed.xml&#39;,&#39;输入&#39;:你&#39; text / plain&#39;,&#39; value&#39;:u&#39; ; SABC新闻&#39;,&#39;语言&#39;:无},&#39; href&#39;:你&#39; http://www.sabc.co.za/wps/PA_News/images/newslogo2.jpg&#39;,&#39;链接&#39; :你&#39; http://www.sabc.co.za/news/&#39;,&#39; subtitle_detail&#39;:{&#39; base&#39;:你&#39; http://www.sabc.co.za/SABC/RSS/news/TopStoryRSSFeed.xml&#39;,&# 39;键入&#39;:你的文字/ html&#39;,&#39;价值&#39;:你&#39; SABC新闻&#39;,&#39;语言&#39;:无}} < / p>
但是当我尝试将其保存到我的数据库时,我收到此错误:
Traceback (most recent call last):
File "C:/Users/les/Desktop/rss2.py", line 18, in <module>
cursor.execute("""INSERT INTO zed (title, description, link, image) VALUES
(%s,%s,%s,%s)""", (d.entries[i].title, d.entries[i].description,
d.entries[i].link, d.entries[i].image))
File "C:\Python27\lib\site-packages\feedparser.py", line 416, in __getattr__
raise AttributeError, "object has no attribute '%s'" % key
AttributeError: object has no attribute 'image'
我使用的代码如下:
import feedparser, MySQLdb
我建立了与MySQL数据库的连接
db = MySQLdb.connect(host='localhost',user='root',passwd='',db='rss')
cursor=db.cursor()
获取Feed并转换为feedparser对象
d=feedparser.parse('http://www.sabc.co.za/SABC/RSS/news/TopStoryRSSFeed.xml')
确定Feed中的条目数,用于处理循环
x = len(d.entries)
处理循环 - 为每个条目选择某些属性并将它们插入MySQL表中。还将RSS输入日期转换为MySQL日期
for i in range(x):
d2 = d.entries[i].description
cursor.execute("""INSERT INTO zed (title, description, link, image)
VALUES (%s,%s,%s,%s)""", (d.entries[i].title, d.entries[i].description,
d.entries[i].link, d.entries[i].image))
db.commit()
他可能有什么问题?
答案 0 :(得分:1)
我认为你应该替换
d.entries[i].image
与
d.entries[i].links[1].href