需要安装的库
pip install bs4
pip install requests
pip install chardet
Beautifulsoup如果安装出问题,可以试试以下的
pip3 install Beautifulsoup4
界面(基于tkinter)
源码
from bs4 import BeautifulSoup
import requests
import tkinter as tk
from tkinter import *
import chardet
window = tk.Tk()
window.geometry("670x460+400+130")
window.title('Url源码解析')
URLtip = Label(window, text='Url')
Sourcetip = Label(window, text="源码")
Transtip = Label(window, text='文字')
URLbox = Text(window, height=1)
StartBtn = Button(window, text='go', command=lambda: get_text_from_url(URLbox.get("1.0", "end-1c")))
deleteBtn = Button(window, text='Reset', width=6, height=1, command=lambda: deleteText())
sourcebox = Text(window, height=14)
source_scroll = Scrollbar(window, command=sourcebox.yview)
sourcebox.config(yscrollcommand=source_scroll.set)
Tranbox = Text(window, height=14)
tran_scroll = Scrollbar(window, command=Tranbox.yview)
Tranbox.config(yscrollcommand=tran_scroll.set)
def get_text_from_url(url):
# HTML source
response = requests.get(url)
response.raise_for_status() # failure message
encoding = chardet.detect(response.content)['encoding']
html = response.content.decode(encoding) # 使用chardet库自动识别编码方式,防止乱码
sourcebox.insert("1.0", html)
# html to text
soup = BeautifulSoup(html, 'lxml') # 自带的parser或者lxml
text = soup.get_text()
Tranbox.insert('1.0',text)
URLtip.grid(row=0, column=0)
URLbox.grid(row=0, column=1)
StartBtn.grid(row=0, column=2)
deleteBtn.grid(row=6, column=1)
Sourcetip.grid(row=2, column=0)
sourcebox.grid(row=2, column=1)
source_scroll.grid(row=2, column=2, sticky='ns')
Transtip.grid(row=4, column=0)
Tranbox.grid(row=4, column=1)
tran_scroll.grid(row=4, column=2, sticky='ns')
def deleteText():
sourcebox.delete("1.0", "end")
Tranbox.delete("1.0", "end")
URLbox.delete("1.0", "end")
window.mainloop()