标签:文本框 root and def mozilla html chrome com apple
所需要库:thinter,python3自带
代码:
from tkinter import * import re import requests def input1(): link = str(inp1.get()) headers = {‘user-agent‘: ‘Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36‘} r = requests.get(link, headers=headers) html = r.text post = re.findall(‘<span class="post-view-count">(.*?)</span>‘, html) txt.insert(END, post) # 追加显示运算结果 inp1.delete(0, END) root = Tk() root.geometry(‘460x240‘) root.title(‘爬取阅读数界面‘) lb1 = Label(root, text=‘请输入需要爬取的网页‘) lb1.place(relx=0.1, rely=0.1, relwidth=0.8, relheight=0.1) inp1 = Entry(root) inp1.place(relx=0.1, rely=0.2, relwidth=0.8, relheight=0.1) # 方法 btn1 = Button(root, text=‘开始爬取‘, command=input1) btn1.place(relx=0.1, rely=0.3, relwidth=0.8, relheight=0.2) # 在窗体垂直自上而下位置60%处起,布局相对窗体高度40%高的文本框 txt = Text(root) txt.place(rely=0.6, relheight=0.4) root.mainloop()
相关参数,参考:https://www.jianshu.com/p/91844c5bca78
原理:想给爬虫一个交互界面,简单的用tkinter做到接收输入,按钮调用,最后输出的结果
实现结果因网页结构而异
实现截图:
标签:文本框 root and def mozilla html chrome com apple
原文地址:https://www.cnblogs.com/echoDetected/p/13236170.html