网上找的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()
答
把你的代码改了一遍。 基本能工作了。
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()
答
看下是不是内容中有中文,而你的python没有设置编码
# -*- coding: utf-8 -*-
或者你的word有换行,没有显示完整。