我试图从web中读取
python模块中的一些数据.
我设法阅读,但在解析这些数据和获取所需信息方面遇到一些困难.
我的代码如下.任何帮助表示赞赏.
#!/usr/bin/python2.7 -tt
import urllib
import urllib2
def Connect2Web():
aResp = urllib2.urlopen("https://uniservices1.uobgroup.com/secure/online_rates/gold_and_silver_prices.jsp");
web_pg = aResp.read();
print web_pg
#Define a main() function that prints a litte greeting
def main():
Connect2Web()
# This is the standard boilerplate that calls the maun function.
if __name__ == '__main__':
main()
当我打印这个web page时,我打印了整个网页.
我想从中提取一些信息,(例如“SILVER PASSBOOK ACCOUNT”并从中获取费率),我在解析这个html文档时遇到了一些困难.
可以使用regexp来获取所需的数据:
import urllib
import urllib2
import re
def Connect2Web():
aResp = urllib2.urlopen("https://uniservices1.uobgroup.com/secure/online_rates/gold_and_silver_prices.jsp");
web_pg = aResp.read();
pattern = "
SILVER PASSBOOK ACCOUNT" + "(.*)" * 4m = re.search(pattern, web_pg)
if m:
print "SILVER PASSBOOK ACCOUNT:"
print "\tCurrency:", m.group(1)
print "\tUnit:", m.group(2)
print "\tBank Sells:", m.group(3)
print "\tBank Buys:", m.group(4)
else:
print "Nothing found"
如果您在循环中进行匹配,请不要忘记重新编译模式.