我有表达正确的寻找愿望变量,但我只希望打印第二个结果,然后第三个结果,依此类推。有没有更好的方法呢?也许添加到SQL数据库并从中调用它?
在窗口和源中显示这些结果的最终目标是RSS提要,不需要获得前2个结果,因此我希望像range
命令那样可以选择第三个结果,例如[2]
。
str1 = open("source/news.xml")
file_contents = str1.read()
title=file_contents.find('<item><title>(.+?)<\/title>')
j = 2
while i < 10:
i = i+1
trending = Label(the_window2, text=title[j+1], font=('Arial', 12))
trending.grid(column = 1, row = i, padx = 2, columnspan=4)
或
使用xml.etree
xmltext = ET.parse('source/old_news.xml')
root = xmltext.getroot()
for title in root:
print(title.attrib)
SQL方法:
我似乎无法使SQL循环工作为错误:"TypeError: string indices must be integers"
connection = connect(database = 'source/old_news.db')
news = connection.cursor()
str1 = open("source/old_news.xml")
file_contents = str1.read()
for item in file_contents['items']:
title = item.title
link = item.link
description = item.description
print (title)
print (link)
print (description)
connection.execute("INSERT INTO ini (title, link, description) VALUES (%s, %s, %s)", (title, link, description))
print ('Succesfull!')