shzaoht 2020-03-18 20:01 采纳率: 100%
浏览 246
已采纳

网上找的python爬取百度百科的代码,加工了一下想自己将print文字转换到tkinterlabel上,但文字就是显示不出来,求大佬解答!

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()
  • 写回答

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()
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题