我有一个任务,我们使用正则表达式从HTML中列出10件事情
我设法从一个网站上列出了10件东西,但在tkinter上显示时,我需要它们旁边的数字
我知道我需要使用for循环,但我不知道如何使用,并且已经研究了很长一段时间
以下是我的一个窗口的代码:
def hidden_gems_on_disney_window():
hidden_gems_on_disney = Tk()
hidden_gems_on_disney['bg'] = 'pink'
hidden_gems_on_disney.title('10 Hidden Gems On Disney+')
hidden_gems_on_disney.geometry('500x500')
Label(hidden_gems_on_disney, text = '10 Hidden Gems On Disney+ You Totally Missed', bg = 'SeaGreen2', font = ('Calibri', 15)).place(x=50, y=45)
url = 'https://screenrant.com/disney-plus-hidden-gems/'
web_page = urlopen(url)
web_page_contents = web_page.read().decode('UTF-8')
#the pattern that I found to list my top 10.
regex = r'(?<=</span>)(.*)(?=</h2>)'
#-------------------------------------------
disney_plus = re.findall(regex, web_page_contents)
x_coord = 40
y_coord = 90
#This for loop shows top 10 contents on this website. There are already just 10 names, so in this case, it is not neccasery to use counter.
for hidden_gems in disney_plus:
Label(hidden_gems_on_disney, text = hidden_gems, bg ='pink', font = ('Malgun', 10)).place(x= x_coord, y= y_coord)
y_coord = y_coord + 38
#Label for the hyperlink. Once clicked, it opens a browser and shows the webpage where data comes from.
most_popular_songs_link = Label(hidden_gems_on_disney, text = 'https://screenrant.com/disney-plus-hidden-gems/', fg='dark green')
most_popular_songs_link.place(x = 110, y = 475)
most_popular_songs_link.bind('<Button-1>', lambda e: callback(url))
mainloop()
由于这个网站排名10到1,我想显示10到1之间的数字,但如果有任何不同,我也想看看如何处理1到10之间的数字
在循环中添加一个计数器
如果要增加计数器,请使用
counter=1
初始化,并使用counter = counter +1
递增您可以在
for
循环中使用enumerate()
函数:相关问题 更多 >
编程相关推荐