from tkinter import *
import re
import bs4
import urllib.request
from bs4 import BeautifulSoup
import urllib.parse
import sys
root = Tk()
root.title('百度百科爬虫')
root.geometry('600x300')
def 搜索():
get1 = enter.get()
get2 = get1.strip()
while get2 != 'out':
if get2 == 'out':
exit(0)
print("please wait...")
try:
url = 'https://baike.baidu.com/item/' + urllib.parse.quote(get2)
html = urllib.request.urlopen(url)
content = html.read().decode('utf-8')
html.close()
soup = BeautifulSoup(content, "html.parser")
text = soup.find('div', class_="lemma-summary").children
print("search result:")
for x in text:
word = re.sub(re.compile(r"<(.+?)>"), '', str(x))
words = re.sub(re.compile(r"\[(.+?)\]"), '', word)
#这里print可以正常显示
print(words)
#下一步想将words用label转换到窗口上时无法显示
lab = Label(root, text=words, font=('微软雅黑', 10), width=400, height=50)
lab.place(relx=0.28, y=100, relheight=0.35, width=400)
except AttributeError:
print("Failed!Please enter more in details!")
return
#输入内容后爬取百度百科相关内容
enter = Entry(root, font=('微软雅黑', 20))
enter.place(relx=0.27, y=230, relheight=0.13, width=400)
button = Button(root, text='发送')
button.place(relx=0.8, y=230, relheight=0.13, width=100,command=搜索)
root.mainloop()
网上找的python爬取百度百科的代码,加工了一下想自己将print文字转换到tkinterlabel上,但文字就是显示不出来,求大佬解答!
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
2条回答 默认 最新
- 陈年椰子 2020-03-19 13:26关注
把你的代码改了一遍。 基本能工作了。
from tkinter import * import re import bs4 import urllib.request from bs4 import BeautifulSoup import urllib.parse import sys #输入内容后爬取百度百科相关内容 if __name__=="__main__": def bksearch(): get1 = enter.get() get2 = get1.strip() # while get2 != 'out': if get2 == 'out': exit(0) print("please wait...") try: url = 'https://baike.baidu.com/item/' + urllib.parse.quote(get2) html = urllib.request.urlopen(url) content = html.read().decode('utf-8') html.close() soup = BeautifulSoup(content, "html.parser") text = soup.find('div', class_="lemma-summary").children print("search result:") for x in text: word = re.sub(re.compile(r"<(.+?)>"), '', str(x)) words = re.sub(re.compile(r"\[(.+?)\]"), '', word) if len(words) < 5: continue # 这里print可以正常显示 print(words) # ret_show.delete(1.0,"end") ret_show.insert("end", words.strip()) # 下一步想将words用label转换到窗口上时无法显示 # lab = Label(root, text=words, font=('微软雅黑', 10), width=400, height=50) # lab.place(relx=0.28, y=100, relheight=0.35, width=400) ret_show.insert("end", "\n") except Exception as e: print("Failed!Please enter more in details!") print(repr(e)) root = Tk() root.title('百度百科爬虫') root.geometry('600x300') enter = Entry(root, font=('微软雅黑', 20)) enter.place(x=1, y=20, relheight=0.13, width=200) ret_show = Text(root, font=('微软雅黑', 10)) ret_show.place(x=1, y=80, relheight=0.5, width=560) button = Button(root, text='发送',command=bksearch) button.place(x=210, y=20, relheight=0.13, width=100) root.mainloop()
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 3无用 1
悬赏问题
- ¥15 CATIA有些零件打开直接单机确定终止
- ¥15 请问有会的吗,用MATLAB做
- ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
- ¥15 ARIMA模型时间序列预测用pathon解决
- ¥15 孟德尔随机化怎样画共定位分析图
- ¥18 模拟电路问题解答有偿速度
- ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
- ¥15 误删注册表文件致win10无法开启
- ¥15 请问在阿里云服务器中怎么利用数据库制作网站
- ¥60 ESP32怎么烧录自启动程序,怎么查看客户esp32板子上程序及烧录地址