How can I sort the results of the two loops in order of time? They are two different loops, but I would like to order them together by mixing the first loops and the second loops in order to sort everything in time. Basically this code scrapes the time and title of some news and prints them in a textbox. They are two different loops.
I wrote this code, but it doesn't work
from datetime import datetime
allnews = news1, news2
allnews.sort()
I have no errors, but I get this output for example:
14.24 TEXAS RANGER #this is loop n.1
14.01 TEXAS RANGER #this is loop n.1
14.20 DETROIT #this is loop n.2
14.13 DETROIT #this is loop n.2
I want to get:
14.24 TEXAS RANGER
14.20 DETROIT
14.13 DETROIT
14.01 TEXAS RANGER
Here is the part of my code useful for solving the question. I do not glue the entire code, but I only glue the piece thanks to which I print
#textbox
textbox = tk.Listbox(window, width=80, height=20, font=('helvetic', 12), selectbackground="#960000", selectforeground="white", bg="white")
textbox.place(x=1, y=1)
def titoli():
#code of scraping....
#TEXAS RANGER
site_texasrangers = requests.get('....')
soup = BeautifulSoup(site_texasrangers.content, 'html.parser')
news = soup.find_all('div', attrs={"class": "tcc-list-news"})
for each in news:
for div in each.find_all("div"):
time= (div.find('span', attrs={'class': 'hh serif'}).text)
tile=(" ".join([span.text for span in div.select("a > span")]))
news1 = (f" {time} {'TEXAS RANGER'}, {title}")
textbox.insert(tk.END, news1)
#DETROIT TIGERS
site_detroit = requests.get('.....')
soup = BeautifulSoup(site_detroit.content, 'html.parser')
news = soup.find_all('div', attrs={"class": "tcc-list-news"})
for each in news:
for div in each.find_all("div"):
time= (div.find('span', attrs={'class': 'hh serif'}).text)
title=(" ".join([span.text for span in div.select("a > span")]))
news2 = (f" {time} {'DETROIT'}, {title}")
textbox.insert(tk.END, news2)
#sorted
from datetime import datetime
allnews = news1, news2
allnews.sort()
titoli()
window.mainloop()
CodePudding user response:
You do it in wrong order. First you have to create empty list, next in loops you have to append news to this list (without displaying), after loop you have to sort list (with reverse=True
), and later use loop to add values to textbox
Something like this
def titoli():
# --- before loops ---
allnews = []
# --- loops ---
#TEXAS RANGER
site_texasrangers = requests.get('....')
soup = BeautifulSoup(site_texasrangers.content, 'html.parser')
news = soup.find_all('div', attrs={"class": "tcc-list-news"})
for each in news:
for div in each.find_all("div"):
time = div.find('span', attrs={'class': 'hh serif'}).text
title = " ".join([span.text for span in div.select("a > span")])
news = f" {time} {'TEXAS RANGER'}, {title})"
allnews.append(news) # <-- append to list without displaying
#DETROIT TIGERS
site_detroit = requests.get('.....')
soup = BeautifulSoup(site_detroit.content, 'html.parser')
news = soup.find_all('div', attrs={"class": "tcc-list-news"})
for each in news:
for div in each.find_all("div"):
time = div.find('span', attrs={'class': 'hh serif'}).text
title = " ".join([span.text for span in div.select("a > span")])
news = f" {time} {'DETROIT'}, {title})"
allnews.append(news) # <-- append to list without displaying
# --- after loops ---
allnews.sort(reverse=True)
for news in allnews:
textbox.insert(tk.END, news)