如果我从一个位置抓取图像,我能够显示图像,但我需要将图像与网站上的相关文章一起更新。因此,如果您明天打开我的应用程序,则会出现不同的图像。所以不用说,我使用正则表达式,但图像只是在标签上显示。到目前为止,这是我的代码:
from Tkinter import *
import Tkinter as tk
import re
from re import findall
from urllib import urlopen
import datetime
from PIL import Image
from PIL import Image, ImageTk
from cStringIO import StringIO
root = Tk()
root.title("Science and Technology")
root.geometry("600x600")
main_label = Label(root, text = "Science and Technology")
main_label.pack()
all_image_height = 400
all_image_width = 400
dash = "-"
now = datetime.datetime.now()
text_area = LabelFrame(root, borderwidth = 2)
text_area.pack(padx = 10, pady = 10, expand = 1, fill= BOTH)
date = Label(text_area)
date.pack()
content_t = Label(text_area)
content_t.pack()
imag = Label(text_area)
imag.pack()
def science_all():
sw_img = 300
sh_img = 300
webScience = urlopen(urlScience)
html_code = webScience.read()
webScience.close()
titl = findall("'name'>(.+[a-zA-Z])</title>", html_code)[0]
date1 = str(now.day),(dash),(now.month),(dash),(now.year)
content_title = findall('itemprop="name">(.+[a-zA-Z])</h5>',html_code)[0]
text_area.config(text = titl)
date.config(text = date1)
content_t.config(text = content_title)
url2 = "http://www.wired.com/"
im = urlopen(url2)
html_code2 = im.read()
im.close()
global image_file
global photo_image
global imag
imag2 = findall('<img src="(.*)">', html_code2)
image_file = Image.open(StringIO(html_code2))
photo_image = ImageTk.PhotoImage(image_file)
imag.config(image=imag2)
Science = Button(root, text="Science",
command = science_all)
Science.pack(side=LEFT)
urlScience = 'http://www.wired.com/'
root.mainloop()
当使用网站中某个特定图像的链接时,它可以正常工作,但是当正则表达式进入时,这对我来说很难。任何建议都会非常有帮助。感谢。