将用户id映射到dict,比较用户是否已经在di中

2024-03-29 13:51:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我现在有一些代码,可以从一个XML文件(从一个网站获得)中打印出每个用户的数据,随着越来越多的用户在一天中与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)

我被告知,这可以通过引用来实现:“将用户id映射到包含用户数据的对象上的dict。在每次运行scraper时,检查用户id是否已经存在于dict中,如果已经存在,则更新相应的对象,否则在dict中添加一个新的条目。“如果有人可以为此类问题提供一些示例代码,我将能够设计它来为我的代码提供解决方案。你知道吗

致以亲切的问候和衷心的感谢 AEA公司


Tags: theto代码用户fromimportreevent
1条回答
网友
1楼 · 发布于 2024-03-29 13:51:23

可以跟踪已经在列表中输出的user_id,但是只记住上一次离开的列表中的位置可能更容易,也就是说,如果在上一次运行中已经输出了前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"]

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

my_set.update(username)

并检查用户是否已通过

if username in my_set:
    ...

相关问题 更多 >