鱼C论坛

 找回密码
 立即注册
查看: 3307|回复: 9

使用tkinter获取爬取到的信息

[复制链接]
发表于 2016-12-23 15:44:44 | 显示全部楼层 |阅读模式
30鱼币
本帖最后由 304079992 于 2016-12-25 21:40 编辑

想实现的是,在爬去页面内容的过程中,同时也输出内容到tkinter界面中。
想问的问题有两个,
1、爬去的过程中,tkinter界面会变成假死状态
2、我只知道用return返回数据到tkinter里,但是return在for循环中会跳出只输出了一句
请各位大神指点下,给点思路,本人新手刚学
  1. import urllib.request
  2. import json
  3. def ShopData(end_page):
  4.     for page in range(1,end_page+1):
  5.         # 商品链接
  6.         ShopUrl = 'http://api.dataoke.com/index.php?r=Port/index&type=total&appkey=w0q7z4fws7&v=2&page='+'end_page'
  7.         # 获取页面数据并且进行JSON转码
  8.         GoodsJson = json.loads(urllib.request.urlopen(ShopUrl).read().decode("utf-8"))
  9.         # print(GoodsJson)
  10.         '''
  11.         上面GoodsJson可拆分写成一下代码
  12.         #打开商品链接
  13.         goods = urllib.request.urlopen(shopUrl)
  14.         #读取并且转码存储到
  15.         goodsData = goods.read().decode("utf-8")
  16.         #对Json数据进行解码
  17.         goodsJson = json.loads(goodsData)
  18.        '''
  19.         for id in range(100):
  20.             #获取所需要的数据
  21.             title = GoodsJson["data"]["pageList"][id]["title"]
  22.             print("商品标题:", title)
  23.             #return title
复制代码

  1. #!/usr/bin/env python
  2. #-*- coding:utf-8 -*-
  3. from main import *
  4. import os, sys
  5. try:
  6.     from tkinter import *
  7. except ImportError:  #Python 2.x
  8.     PythonVersion = 2
  9.     from Tkinter import *
  10.     from tkFont import Font
  11.     from ttk import *
  12.     #Usage:showinfo/warning/error,askquestion/okcancel/yesno/retrycancel
  13.     from tkMessageBox import *
  14.     #Usage:f=tkFileDialog.askopenfilename(initialdir='E:/Python')
  15.     #import tkFileDialog
  16.     #import tkSimpleDialog
  17. else:  #Python 3.x
  18.     PythonVersion = 3
  19.     from tkinter.font import Font
  20.     from tkinter.ttk import *
  21.     from tkinter.messagebox import *
  22.     #import tkinter.filedialog as tkFileDialog
  23.     #import tkinter.simpledialog as tkSimpleDialog    #askstring()

  24. class Application_ui(Frame):
  25.     #这个类仅实现界面生成功能,具体事件处理代码在子类Application中。
  26.     def __init__(self, master=None):
  27.         Frame.__init__(self, master)
  28.         self.master.title('Form1')
  29.         self.master.geometry('665x362')
  30.         self.createWidgets()

  31.     def createWidgets(self):
  32.         self.top = self.winfo_toplevel()

  33.         self.style = Style()

  34.         self.Text1Var = StringVar(value='')
  35.         self.Text1 = Entry(self.top, textvariable=self.Text1Var, font=('宋体',9))
  36.         self.Text1.place(relx=0.144, rely=0.044, relwidth=0.098, relheight=0.069)

  37.         self.style.configure('Label1.TLabel',anchor='w', font=('宋体',9))
  38.         self.Label1 = Label(self.top, text='抓取页数', style='Label1.TLabel')
  39.         self.Label1.place(relx=0.06, rely=0.066, relwidth=0.074, relheight=0.047)

  40.         self.style.configure('Command1.TButton',font=('宋体',9))
  41.         self.Command1 = Button(self.top, text='开始', command=self.Command1_Cmd, style='Command1.TButton')
  42.         self.Command1.place(relx=0.253, rely=0.044, relwidth=0.098, relheight=0.091)

  43.         self.Text2Var = StringVar(value='')
  44.         self.Text2 = Entry(self.top, textvariable=self.Text2Var, font=('宋体',9))
  45.         self.Text2.place(relx=0.024, rely=0.221, relwidth=0.952, relheight=0.732)


  46. class Application(Application_ui):
  47.     #这个类实现具体的事件处理回调函数。界面生成代码在Application_ui中。
  48.     def __init__(self, master=None):
  49.         Application_ui.__init__(self, master)

  50.     def Command1_Cmd(self, event=None):
  51.         #TODO, Please finish the function here!
  52.         end_page = self.Text1.get()

  53.         self.Text2.insert(END,ShopData(int(end_page)))
  54.         self.top.update()
  55. if __name__ == "__main__":
  56.     top = Tk()
  57.     Application(top).mainloop()
  58.     try: top.destroy()
  59.     except: pass
复制代码
QQ图片20161223153334.png

最佳答案

查看完整内容

多线程下载图片,以及 界面互动~ http://bbs.fishc.com/thread-74052-1-1.html (出处: 鱼C论坛)
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2016-12-23 15:44:45 | 显示全部楼层
多线程下载图片,以及 界面互动~
http://bbs.fishc.com/thread-74052-1-1.html
(出处: 鱼C论坛)
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2016-12-23 17:19:55 | 显示全部楼层
SixPy 发表于 2016-12-23 16:54
多线程下载图片,以及 界面互动~
http://bbs.fishc.com/thread-74052-1-1.html
(出处: 鱼C论坛)

好的,谢谢,我看看
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2016-12-23 21:20:56 | 显示全部楼层
不错
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2016-12-23 21:50:28 | 显示全部楼层
厉害
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

头像被屏蔽
发表于 2016-12-23 22:23:17 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2016-12-24 10:03:38 | 显示全部楼层
厉害厉害
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

头像被屏蔽
发表于 2016-12-24 23:40:03 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2016-12-25 17:36:34 | 显示全部楼层
SixPy 发表于 2016-12-23 15:44
多线程下载图片,以及 界面互动~
http://bbs.fishc.com/thread-74052-1-1.html
(出处: 鱼C论坛)

再请教下:tk组件中的  Text和Scrollbar组合的时候,怎么才能使滚动条定位到最新插入行的位置上呢?
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2016-12-29 15:46:48 | 显示全部楼层
304079992 发表于 2016-12-25 17:36
再请教下:tk组件中的  Text和Scrollbar组合的时候,怎么才能使滚动条定位到最新插入行的位置上呢?

name.see(END)  就可以查看到最新插入一行的文字了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-2-24 15:02

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表