此代码循环访问RSS源以获取新闻摘要。摘要的长度和变化都有所不同。
由于我使用每个循环row = i + 5
的简单偏移来填充这些,wraplength
选项有时会导致前一行超过5行,导致偏移在写下面的下一个故事时失败前一个。
有没有一种方法可以引用最近使用过的行,只是让下一个故事填充在下面?
section_start = 9
news_root = Frame(root, bg = 'green').grid(row = section_start, column = 0, columnspan = 4, rowspan = 20, sticky = W)
url = 'http://feeds.nytimes.com/nyt/rss/Business'
response = requests.get(url)
soup = BeautifulSoup(response.content, features = 'xml')
items = soup.findAll('item')
for item in items[:5]:
i += 2
news_title = Label(news_root, text = item.title.text, fg = 'mediumblue', bg = 'white', font = ('Calibri',10))
new_title.grid(row = i, column = 0, columnspan = 4, rowspan =4, sticky = 'W')
news_des = Label(news_root, text = item.description.text, fg = 'dimgray', bg = 'white', font = ('Calibri',8), wraplength = 500, justify = LEFT)
news_des.grid(row = i+5, column = 0, rowspan =4, columnspan = 10, sticky = W)