gpt4 book ai didi

python - 如何使 rp 和 xrange 动态?

转载 作者:太空宇宙 更新时间:2023-11-04 08:14:30 26 4
gpt4 key购买 nike

嘿伙计们,非常感谢你花时间看我的问题,我已经在这段代码上工作了大约 1 周(我是编码新手,也是 python 1 周)目前只有 x in xrange 时循环才有效(x) 和 'rp':'x' 是此 xml 中可用的正确行数。 xml全天更新,我想知道是否有人可以提供使x动态化的解决方案?

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

from sched import scheduler
from time import time, sleep

s = scheduler(time, sleep)

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

def getData():
post_url = "urlofinterest_xml"
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' : '8',
'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>')
pattern4 = re.compile('title=\'Naps posted: (.*) Winners:')
pattern5 = re.compile('Winners: (.*)\'><img src=')


for i in xrange(8):
user_delimiter = xmlload1['rows'][i]['cell']['username']
selection_delimiter = xmlload1['rows'][i]['cell']['race_horse']

username_delimiter_results = re.findall(pattern1, user_delimiter)[0]
userid_delimiter_results = int(re.findall(pattern2, user_delimiter)[0])
user_selection = re.findall(pattern3, selection_delimiter)[0]
user_numberofselections = float(re.findall(pattern4, user_delimiter)[0])
user_numberofwinners = float(re.findall(pattern5, user_delimiter)[0])

strikeratecalc1 = user_numberofwinners/user_numberofselections
strikeratecalc2 = strikeratecalc1*100

print "user id = ",userid_delimiter_results
print "username = ",username_delimiter_results
print "user selection = ",user_selection
print "best price available as decimal = ",xmlload1['rows'][i]['cell'] ['tws.best_price']
print "race time = ",xmlload1['rows'][i]['cell']['race_time']
print "race meeting = ",xmlload1['rows'][i]['cell']['race_meeting']
print "ROI = ",xmlload1['rows'][i]['cell']['roi']
print "number of selections = ",user_numberofselections
print "number of winners = ",user_numberofwinners
print "Strike rate = ",strikeratecalc2,"%"
print ""


getData()


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

亲切的问候AEA

最佳答案

首先,我将讨论如何迭代结果。根据您的代码,xmlload1['rows'] 是一个字典数组,因此您可以直接遍历它,而不是选择任意数字。为了使这个示例更好,我将设置一些任意数据来说明这一点:

xmlload1 = {
"rows": [{"cell": {"username": "one", "race_horse":"b"}}, {"cell": {"username": "two", "race_horse": "c"}}]
}

因此,根据上面的数据,您可以在 for 循环中遍历行,如下所示:

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

每次迭代,单元格都采用可迭代对象中另一个元素的值(xmlload1['rows'] 中的列表)。这适用于支持迭代的任何容器或序列(如 liststuplesdictsgenerators 等。 )

请注意,我没有在任何地方使用任何魔数(Magic Number),所以 xmlload1['rows'] 可以是任意长的,它仍然有效。

您可以使用函数将请求设置为动态的,如下所示:

def get_data(rp=8, page=1):
parameters = {'page' : str(page),
'rp' : str(rp),
'sortname' : 'roi',
'sortorder' : 'desc'
}
data = urllib.urlencode(parameters)
trans_array = browser.open(post_url,data).read().decode('UTF-8')
return json.loads(trans_array)

现在,您可以调用 get_data(rp=5) 获取 5 行,或调用 get_data(rp=8) 获取 8 行 [and get_data (rp=8, page=3) to get the third page], 等等。你可以清楚地添加额外的变量,甚至直接将 parameters dict 传递给函数。

关于python - 如何使 rp 和 xrange 动态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16879848/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com