狗仔小分队的博客

  • 服务器
    • 干货推荐
    • 教程&一键脚本
    • 特价监控
  • 实用工具
    • 小工具汇总
  • 教程
    • Ubuntu
    • Python
    • 学习
  • 福利
    • 商城
    • 薅羊毛
    • 影视&资源推荐
  1. 首页
  2. 教程
  3. Python
  4. 正文

论坛新帖检测 --hostloc论坛

2017年4月1日 4742点热度 0人点赞 0条评论

[title]介绍[/title]

为了省事儿,简单写了一个检测hostloc论坛新帖子的脚本

界面使用了python自带的tkinter,还是蛮简单的,代码比较水

  • python脚本,windows下测试通过
  • 需要配置python包,安装requests库和lxml库
  • 或者直接安装Anaconda3环境

[title]使用说明[/title]

  • 第一步配置环境
  • 下载代码段,然后python loc.pyw运行脚本

[title]支持[/title]

  • 点击列表自动打开浏览器,访问页面
  • 点击On-Top,切换窗口是否保持置顶

[title]其他说明[/title]

  • 当前10s更新一次数据
  • 同目录下会生成一个.db的数据库文件存帖子信息
  • 如有需要自己更改py脚本即可

[title]示例[/title]

[title]代码[/title]:懒人下载链接:

import tkinter
import tkinter.messagebox
import time
import random
import _thread

class Menu:

    def __init__(self):    

        self.main_window = tkinter.Tk()
        self.main_window.title("Auto Show Data")
        self.main_window.geometry("300x250")

        self.ListBox = tkinter.Listbox(self.main_window)        
        self.ListBox.pack()
        self.ListBox.place(x=0,y=0,height=200,width=600)
        self.ListBox.bind('<<ListboxSelect>>', self.OnList)

        self.ontopstatus = True
        self.quitButton = tkinter.Button(self.main_window,\
                                          text = 'On-Top',
                                          command = self.OnTop,\
                                          height = 2, width = 6)
        self.quitButton.pack()
        self.quitButton.place(x=50, y=200)

        self.main_window.after(100, _thread.start_new_thread, self.BgRun, ()) # first parameter time(ms)
        self.main_window.after(100, _thread.start_new_thread, self.RunMoniter, ()) # first parameter time(ms)
        tkinter.mainloop()

    def OnTop(self):
        print(self.ontopstatus)
        self.main_window.wm_attributes('-topmost',self.ontopstatus)
        self.ontopstatus = not self.ontopstatus

    def OnList(self,event):
        print(self.ListBox.curselection())
        print(self.ListBox.get(self.ListBox.curselection()))
        cont=self.ListBox.get(self.ListBox.curselection())
        import os
        os.popen(os.popen('start http://www.hostloc.com/thread-%s-1-1.html'%cont.split(']')[0][1:]))

    def BgRun(self):
        self.ListBox.insert(0, "Lasted Updated Time : " + str(time.ctime()).split()[3])
        while(1):
            try:
                self.ListBox.delete(0)
                self.ListBox.insert(0, "Lasted Updated Time : " + str(time.ctime()).split()[3])
                listret = cjloc()
                for ret in listret:
                    self.ListBox.insert(1, '[%s]%s'%(ret[0].split('-')[1],ret[1]))
                self.ListBox.delete(0)
                self.ListBox.insert(0, "Lasted Updated Time : " + str(time.ctime()).split()[3] + "  Success!")
            except Exception as e:
                print(e)
            time.sleep(10)
    def RunMoniter(self):
        while(1):
            self.main_window.title(str(time.ctime()).split()[3])
            time.sleep(1)

def cjloc():
    import requests
    from lxml import html
    import sqlite3
    namecat='loc'
    urlbase='http://www.hostloc.com/thread-%s-1-1.html'
    urlreq='http://www.hostloc.com/forum-45-1.html'

    conn=sqlite3.connect('cjloc.db')
    # conn.execute('drop table loc')
    # select * from loc order by time desc limit 30
    conn.execute("CREATE TABLE IF NOT EXISTS loc(uid integer unique,title text,content text,time TimeStamp NOT NULL DEFAULT (datetime('now','localtime')))")

    cont=requests.get(urlreq,timeout=10)
    cont=cont.content
    # print(cont)
    econt=html.fromstring(cont)#.decode('utf-8'))
    eclist=econt.xpath('//*[@class="s xst"]')
    listret=list()
    for ec in eclist:
        ecuid = ec.attrib['href'].split('-')[1]
        # print(urlbase%(ecuid,))
        ectext = ec.text
        # print(ectext)
        try:
            conn.execute('insert into loc(uid,title) values(?,?)',[ecuid,ectext])
            print('Add Success -- [%s]%s'%(urlbase%(ecuid,),ectext))
            listret.append((urlbase%(ecuid,),ectext))
        except Exception as e:
            print(e)
        conn.commit()
    conn.close()
    return listret

gui = Menu()

 

标签: python 监控
最后更新:2017年10月14日

xiaofd

这个人很懒,什么都没留下

点赞
< 上一篇
下一篇 >

COPYRIGHT © 2021 xiaofd.net. ALL RIGHTS RESERVED.

THEME KRATOS MADE BY VTROIS