@AndiArbeit: Ich hatte da ja schon einen Beitrag mit Quelltext zu geschrieben.
Edit:
Code: Alles auswählen
import Tkinter as tk
from functools import partial
from io import BytesIO
from itertools import imap
import requests
from bs4 import BeautifulSoup
from PIL import Image, ImageTk
BASE_URL = 'http://www.spiegel.de'
TOP_HEADLINES_URL = BASE_URL + '/schlagzeilen/tops/index.html'
class ArticleButton(tk.Button):
def __init__(self, master, url, headline, image, headline_label):
image = ImageTk.PhotoImage(image)
tk.Button.__init__(self, master, image=image, command=self._clicked)
self.image = image
self.url = url
self.headline = headline
self.headline_label = headline_label
self.bind(
'<Enter>', lambda _: headline_label.configure(text=self.headline)
)
self.bind('<Leave>', lambda _: headline_label.configure(text=''))
def _clicked(self):
print self.url
class ArticlesFrame(tk.Frame):
def __init__(self, master, articles, articles_per_row=6):
tk.Frame.__init__(self, master)
headline_label = tk.Label(self)
headline_label.pack()
button_frame = tk.Frame(self)
for i, (url, headline, image) in enumerate(articles):
button = ArticleButton(
button_frame, url, headline, image, headline_label
)
row, column = divmod(i, articles_per_row)
button.grid(row=row, column=column)
button_frame.pack()
def scrape_headline_link(base_url, link_node):
return (
base_url + link_node['href'],
link_node['title'],
Image.open(BytesIO(requests.get(link_node.img['src']).content))
)
def scrape_headlines(base_url, headlines_url):
return imap(
partial(scrape_headline_link, base_url),
BeautifulSoup(requests.get(headlines_url).text).select(
'div.schlagzeilen-content div.article-image-box a'
)
)
def main():
root = tk.Tk()
articles_frame = ArticlesFrame(
root, scrape_headlines(BASE_URL, TOP_HEADLINES_URL)
)
articles_frame.pack()
root.mainloop()
if __name__ == '__main__':
main()