0

我的代码当前从 XML 文件(从网站获得)中为每个用户打印数据,随着更多用户全天与之交互,XML 会更新。我目前有我的代码循环以每 5 分钟下载一次此数据。

每次运行代码时,它都会生成一个用户列表及其统计信息,前 5 分钟它会打印用户:a,b,c

第二个 5 分钟打印用户:a,b,c,d,e

第三个 5 分钟打印用户:a,b,c,d,e,f,g

我需要代码来打印前 5 分钟:a、b、c 第二 5 分钟:d、e 第三 5 分钟:f、g

一些如何识别一些用户已经被使用。每个用户都有一个唯一的用户 ID,我猜它可以匹配吗?

我附上了我的代码示例,以防万一。

import mechanize
import urllib
import json
import re
import random
import datetime
from sched import scheduler
from time import time, sleep

######Code to loop the script and set up scheduling time

s = scheduler(time, sleep)
random.seed()

def run_periodically(start, end, interval, func):
    event_time = start
    while event_time < end:
        s.enterabs(event_time, 0, func, ())
        event_time += interval + random.randrange(-5, 45)
    s.run()


###### Code to get the data required from the URL desired
def getData():  
    post_url = "URL OF INTEREST"
    browser = mechanize.Browser()
    browser.set_handle_robots(False)
    browser.addheaders = [('User-agent', 'Firefox')]

######These are the parameters you've got from checking with the aforementioned tools
    parameters = {'page' : '1',
              'rp' : '250',
              'sortname' : 'roi',
              'sortorder' : 'desc'
             }
#####Encode the parameters
    data = urllib.urlencode(parameters)
    trans_array = browser.open(post_url,data).read().decode('UTF-8')

    xmlload1 = json.loads(trans_array)
    pattern1 = re.compile('>&nbsp;&nbsp;(.*)<')
    pattern2 = re.compile('/control/profile/view/(.*)\' title=')
    pattern3 = re.compile('<span style=\'font-size:12px;\'>(.*)<\/span>')


#########################################################################
##### The request sent from here all the way down including comments#####
#########################################################################


##### Making the code identify each row, removing the need to numerically quantify the     number of rows in the xmlfile,
##### thus making number of rows dynamic (change as the list grows, required for looping function to work un interupted)

    for row in xmlload1['rows']:
        cell = row["cell"]

##### defining the Keys (key is the area from which data is pulled in the XML) for use in the pattern finding/regex

        user_delimiter = cell['username']
        selection_delimiter = cell['race_horse']


        if strikeratecalc2 < 12 : continue;

##### REMAINDER OF THE REGEX DELMITATIONS
        username_delimiter_results = re.findall(pattern1, user_delimiter)[0]
        userid_delimiter_results = (re.findall(pattern2, user_delimiter)[0])
        user_selection = re.findall(pattern3, selection_delimiter)[0]



##### Printing the results of the code at hand

        print "user id = ",userid_delimiter_results
        print "username = ",username_delimiter_results
        print "user selection = ",user_selection
        print ""





    getData()


    run_periodically(time()+5, time()+1000000, 3000, getData)

请善待评论,我已经累计编码了 11 天,所以也请原谅我正在使用的代码中的任何重大错误,尽管到目前为止它正在工作。

亲切的问候

原子能机构

4

1 回答 1

5

我想你可以简单地将唯一 ID 存储在某个地方(比如文件或数据库 -Redis是我最喜欢的),然后检查它们。

对于与 一起存储Redis,您可以执行以下操作:

# redis
import redis
pwd = 'l33t'
r = redis.StrictRedis(host='localhost', port=6379, db=1, password=pwd)  

# set id's
r.sadd('user_ids', unique_id) # this is a set, with no duplicates

# check for existing id's
r.sismember('user_ids', unique_id) # returns 1 or 0

请参阅http://redis.io/commands#sethttps://github.com/andymccurdy/redis-py。两者都需要Redisredis-py,安装需要两分钟。

于 2013-06-06T00:05:02.750 回答