我能够解析rss提要并将链接,图块和描述成功保存到我的数据库中,但是当我尝试对同一提要中的图像执行相同操作时,出现了错误。我已经检查了xml文件是否具有可通过以下方式使用的类别“图像”:
d.feed.image { '字幕':u'SABC新闻, '链接':[{ 'href'属性:U ' http://www.sabc.co.za/news/ ', '型':U”的text / html ' '相对':u'alternate '}], '标题':u'SABC新闻', '身高':173, '宽度':308, 'title_detail':{ '基地':U' HTTP ://www.sabc.co.za/SABC/RSS/news/TopStoryRSSFeed.xml','type ':u'text / plain','value':u'SABC News','language':None}, 'href':u'http: //www.sabc.co.za/wps/PA_News/images/newslogo2.jpg','link ':u'http: //www.sabc.co.za/news/ ' ,'subtitle_detail':{'base':u'http: //www.sabc.co.za/SABC/RSS/news/TopStoryRSSFeed。xml ','type':u'text / html','value':u'SABC新闻','language':无}}
但是当我尝试将其保存到数据库时,出现此错误:
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()
获取提要并变成feedparser对象
d=feedparser.parse('http://www.sabc.co.za/SABC/RSS/news/TopStoryRSSFeed.xml')
确定提要中的条目数,该条目将在处理循环中使用
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()
他可能是什么问题?
我认为你应该更换
d.entries[i].image
和
d.entries[i].links[1].href
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句