【发布时间】:2014-09-23 15:20:11
【问题描述】:
我目前正在合并这个函数来检查多个 url。它将 html 页面读入字符串并匹配文件传输的进度百分比,如下所示:
def check(server):
logging.info('Fetching {0}.'.format(server))
# Open page
response = urllib2.urlopen("http://"+server+"/avicapture.html")
tall = response.read() # puts the data into a string
html = tall.rstrip()
# Grab progress percentage.
match = re.search('.*In Progress \((.*)%\).*', html)
然后在此匹配上,将字符串中的百分比数字返回给父进程。
if match:
global temp
global results
temp = match.group(1)
results = temp
servers[server] = temp
if int(temp) >= 98 and int(temp) <= 99:
abort(server)
alertmail(temp, server)
rem = str(server)
complete(rem)
logging.info('{0} completed.'.format(server))
return str(temp)
但是,有时它不会显示“进行中”并且有一个百分比。它将显示“传输中止”或“就绪”。我将如何构造它以返回它找到的任何一个,正在进行(百分比)、传输中止或就绪?
编辑:我忘了提到我需要它来匹配最近的文件传输,基于结束时间。 (见:http://www.whatdoiknow.net/dump/avicapture_full.html#status)
部分解决方案:
match = re.search('.*In Progress \((.*)%\).*', html)
match2 = re.search('.*Ready.*', html)
match3 = re.search('.*Transfer Aborted.*', html)
if match:
global temp
temp = match.group(1)
if int(temp) >= 98 and int(temp) <= 99:
logging.info('{0} completed.'.format(server))
return str(temp)
elif match2:
temp = "Ready"
logging.info('{0} is ready.'.format(server))
return str(temp)
elif match3:
temp = "Transfer Aborted"
logging.info('{0} was Aborted.'.format(server))
return str(temp)
但是,这并不能满足我识别最近转移的需要..
【问题讨论】:
-
你能提供实际的示例数据吗?
-
当然。这是函数抓取的页面。我们正在查看“Avi 文件状态”部分。我基本上需要它来按日期识别最近的传输,然后检查该传输的“上传状态”列是否具有这三个字符串中的任何一个,并返回我上面的百分比数字,或者“传输中止” ',或'准备好了'whatdoiknow.net/dump/avicapture_full.html