基于BeautifulSoup的url源码解析并提取文字的简单GUI程序

需要安装的库

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()

  • 9
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值