0

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

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

第二个 5 分钟打印用户:

x,y,z,a,b

第三个 5 分钟打印用户:

x,y,z,a,b,c,d

我需要代码来打印前 5 分钟:

x,y,z 

第二个5分钟:

a,b 

第三个5分钟:

c,d

一些如何识别一些用户已经被使用。每个用户都有一个唯一的用户 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>')



##### 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)

我被告知这可以使用引用来实现:“将 user_id 映射到包含用户数据的对象的字典。在每次运行刮板时,检查用户 ID 是否已经在字典中,如果是,则更新相应的对象,否则在字典中添加一个新条目。” 如果有人可以为此类问题提供一些示例代码,我将能够对其进行工程设计,以便为我的代码提供解决方案。

亲切的问候,非常感谢 AEA

4

1 回答 1

3

可以跟踪user_id您已经在列表中输出的 s,但是记住您上次离开的列表中的位置可能更容易——也就是说,如果您已经输出了前 5 个用户在以前的运行中,从下一次运行的第 6 行开始。您可以在循环中实现它,例如:

#outside of the run loop
number_output = 0

#in the run lop
for row in xmlload1['rows'][number_output:]:
    number_output += 1
    cell = row["cell"]

唯一的问题是,如果用户可以在输入文件中重复,并且您不想输出用户的第二个实例。在这种情况下,最好使用set. 然后,每次输出用户时,将其用户名添加到您的集合中,例如

my_set.update(username)

并检查用户是否已经通过输出

if username in my_set:
    ...
于 2013-06-06T14:12:24.057 回答