当前位置:   article > 正文

如何使用python简单的爬取微博搜索的内容_python 获取微博搜索数据页面

python 获取微博搜索数据页面

UI界面输入关键词用python爬取微博内容

第一步:创建main.py文件,用来作为主类。

在main.py文件中进行以下操作。
一、程序入口,明白接下来进行的操作
1、加载UI界面。
2、爬取网页内容。


#程序入口
if __name__ == "__main__":
    t1=threading.Thread(target=thread_one)            #创建t1线程用于加载UI界面
    t1.start()                                        #启动t1线程
 
    t3=threading.Thread(target=thread_three)          #创建t3线程用于检测UI界面中的输入框是否有输入
    t3.start()                                        #启动t3线程
    t4=threading.Thread(target=thread_four)           #创建t4线程用于循环获取UI界面输入框中的内容
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

使用tkinter创建简单的UI界面。

创建UI.py文件
UI界面代码如下:

from tkinter import *


class ui(object):
    test = False
    txt=''
    def loading(self):
        root = Tk()
        root.title("微博爬取器")
        root.geometry("600x400")
        #创建文本框
        return root

       

    def Text(self,root):
        text=Entry(root,bd=0)
        text.place(x=200, y=180, width=175, height=40)
        return text
    
    
    #第一种方法
    # @staticmethod
    # def fuc(text):
    #     # 获取用户输入
    #     txt = text.get()
    #     print(txt)

    
    # 第二种方法
    def fuc(self,text):
        # 获取用户输入
        # ui.txt = text.get()
        print("执行了")

        ui.test=True


    # 创建搜索按钮
    def ButtoN(self,root,text):
        button=Button(root,command=lambda: ui.fuc(self,text))
        button.place(x=390, y=180, width=40, height=40)



    def show(self,root):
        root.mainloop()


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49

完整main.py文件示例如下:

# from selenium import webdriver
# from selenium.webdriver.chrome.options import Options
# from selenium.webdriver.common.keys import Keys
import time,threading
from UI import ui
from lxml import etree
import requests

#加载UI用户界面
list_var=[]                           #list_var用于接收从UI界面中输入框里获取的内容
test=False                            #test用于作为检测是否可以执行thread_two()方法的条件。如果test为True则会被t3线程检测到,从而
                                      #执行thread_two()方法。为False则会不做处理。
list_var1=[]                          #list_var1
def thread_one():
        gc = ui()                     #创建UI文件里的ui类的实例对象gc
        root = ui.loading(gc)         #调用ui类的loading()方法并传入实例对象,返回得到主界面对象root
        global list_var1              #在thread_one()方法中对其声明使用全局成员列表list_var1
        text = ui.Text(gc, root)      #调用ui类的Text()方法传入实例对象gc与主界面对象root,并返回得到文本框控件对象text
        list_var1.append(text)        #将文本框控件对象text添加到列表list_var1中,方便之后使用这个对象
        ui.ButtoN(gc, root, text)     #使用ui类的ButtoN的方法(),并传入实例对象gc、主界面对象root、文本框控件对象text
        t4.start()                    #启动t4线程
        ui.show(gc, root)             #调用ui
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/blog/article/detail/57153
推荐阅读
相关标签
  

闽ICP备14008679号