【发布时间】:2012-08-29 06:45:23
【问题描述】:
我已经在命令行中实现了程序,我编写了这样的代码:
import requests
import re
import sys
amount=[]
l=len(sys.argv)
from_=sys.argv[l-2]
to=sys.argv[l-1]
for i in range(1,l-2):
amount.append(sys.argv[i])
for i in amount:
r = requests.get("http://www.xe.com/ucc/convert/?Amount=%(amount)s&From=%(from_)s&To=%(to)s"%{"amount":i,"from_":from_,"to":to})
#dataCrop=re.findall('[0-9,]+\.[0-9]+',r.text)
dataCrop=re.compile(r'[0-9,]+\.[0-9]+')
for m in dataCrop.finditer(r.text):
print m.group()
当我运行程序时,我得到如下输出:
1.0
1.1
3.43
1.1
1.1
1.1
1999.09
1.1
1999.09
4.7
1.00
55.6565
55.6565
0.0179674
1.00000
0.79649
0.63231
55.6565
0.96365
0.98866
8.42406
1.24172
78.5433
1.00000
1.25551
1.58151
0.01797
1.03772
1.01147
0.11871
0.80533
0.01273
0.01797
0.01431
0.01136
1.00000
0.01731
0.01776
0.15136
0.02231
1.41122
55.6565
69.8771
88.0212
1.00000
57.7556
56.2947
6.60685
44.8219
0.70861
9.4
2.0
但我只想输出'55.6565'(在第12位和第11位)。我应该如何修改我的正则表达式?
【问题讨论】:
-
如果您只想在结果中使用
55.6565,请使用r.text.count('55.6565') -
它的动态程序这个值不断变化。这是“1”的答案。如果我给“2”,那么它将是 111.some 值。
-
55.6565 USD 这是我的答案所在的标签。当我在命令行中给出值 1 即 p programname.py 1 USD INR 时,55.6565 将给出答案。当我给 p programname.py 2 USD INR 时,它会根据货币给出答案 111.385。现在我的问题是如何使用正则表达式提取这个值。我不能用漂亮的汤。有人可以帮我吗? -
您可能应该看看这段文字:警告:使用条款禁止自动提取费率。.
-
The foregoing prohibitions expressly include, but are not limited to, the practice of "screen scraping", or any other practice or activity the purpose of which is to obtain lists of data, portions of a database, or other lists or information from the Services, in any manner or in any quantities not authorized in writing by XE.。也就是说,你需要pay for this kind of stuff。
标签: python regex python-3.x python-2.7