1. 程式人生 > 實用技巧 >Python簡易 爬蟲+圖形化介面

Python簡易 爬蟲+圖形化介面

所需要庫: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做到接收輸入,按鈕呼叫,最後輸出的結果

實現結果因網頁結構而異

實現截圖: