如何使rp和xrange动态?

大家好,感谢您抽出宝贵的时间来研究我的问题,我已经在此代码上工作了大约1周的时间(我也是编码和python的新手,也是1周的时间)。目前,该循环仅在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=')


ibeautiful
浏览 106回答 2
2回答

慕的地10843

我不确定我是否理解您的问题,但是我认为您想要的是:rows = xmlload1['rows']for row in rows:&nbsp; &nbsp; user_delimiter = row['cell']['username']&nbsp; &nbsp; selection_delimiter = row['cell']['race_horse']&nbsp; &nbsp; # ...如果您需要行索引以及行本身,请使用enumerate:rows = xmlload1['rows']for i, row in enumerate(rows):&nbsp; &nbsp; user_delimiter = row['cell']['username']&nbsp; &nbsp; selection_delimiter = row['cell']['race_horse']&nbsp; &nbsp; # ...通常,如果您for i in range(…)出于除固定次数的迭代之外的任何其他目的进行操作,则说明这样做是错误的。通常有一个要迭代的集合;只是找到它并对其进行迭代。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python