• python 爬取糗事百科 gui小程序


    前言:有时候无聊看一些搞笑的段子,糗事百科还是个不错的网站,所以就想用Python来玩一下。也比较简单,就写出来分享一下。嘿嘿

    环境:Python 2.7 + win7

    现在开始,打开糗事百科网站,先来分析。地址:https://www.qiushibaike.com

    一般像这种都是文本的话,查看源代码就可以看到内容了。

    已经可以看到都是在一个class 为content 的div里面,这样就很简单了,直接上正则表达式来匹配就好了。

    <div.*?class="content">(.*?)</div>

     这样等会再代码里面就可以提取出来段子内容了,再来看一下分页。

    分页也很简单,很有规律,直接接上页数就行了。

    OK  既然 都分析完了 那就直接上代码。

    #-*- coding: UTF-8 -*-
    # author : Corleone
    from Tkinter import *
    import urllib2,re
    
    def load(page):
            url="http://www.qiushibaike.com/text/page/"+str(page)+"/?s=4937798"
            user_agent="Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.113 Safari/537.36"
            headers={'User-Agent':user_agent}
            res=urllib2.Request(url,headers = headers)
            html = urllib2.urlopen(res).read()
            reg=re.compile(r'<div.*?class="content">(.*?)</div>',re.S)
            duanzi=reg.findall(html)
            return duanzi
    i=0
    page=1
    def get():
        if i==0:
            txtlist=load(page)
            page+=1
        if i<20:
            txt.delete(1.0,END)
            txt.insert(1.0,txtlist[i].replace("<span>","").replace("</span>","").replace("</br>","").replace("
    ","").replace("<br/>",""))
            i+=1
            global i
            global page
            global txtlist
        else:
            i=0
    
    def main():
        root=Tk()   # 定义一个窗口
        root.title("Corleone") # 定义窗口标题
        root.geometry('500x500')  # 定义窗口大小
        b=Button(root,text="next",width=25,bg="red",command=get)  # 定义一个按钮
        b.pack(side=BOTTOM)  # 按钮的布局 放在窗口最下面
        global txt
        txt=Text(root,font=(u"黑体",20)) # 定义一个编辑界面
        txt.pack(expand=YES,fill=BOTH)  # 编辑界面布局 随窗口大小而自动改变
        root.mainloop()   # 让窗口一直在屏幕上显示出来 
    
    
    main()

    这里用到了Python自带的图形化界面库 Tkinter 来做gui界面。一页大概20个段子 next 按钮 下一个 看完了 就翻页。

    嘿嘿,这样就能直接看了。OK 好了  这篇文章也很简单,没啥技术含量,莫见怪,代码都是我之前写的,现在依然能用,就发出来了 : )

  • 相关阅读:
    Do the “StreamWriter.WriteLine()” function need to “lock()”?
    SQL Server Integration Services C#脚本任务示例 先看前二部分
    使用SSIS脚本任务触发事件,执行T-SQL命令并运行SMO 第二部分
    Getting started with the SSIS Script Task 第一部分
    对SQL Server的监控和报告
    在Visual Studio 2019中安装SQL Server Integration Services
    .Net ObjectContext.CreateQuery<T>(String, ObjectParameter[]) Method
    使用Redgate的SQL Monitor优化SQL Server资产监视
    将多行汇总为SQL Server数据的一行和一列
    用于SQL源的Power BI增量刷新
  • 原文地址:https://www.cnblogs.com/binglansky/p/8512011.html
Copyright © 2020-2023  润新知