加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

从python中读取的网页中读取一些内容

发布时间:2020-12-20 12:19:58 所属栏目:Python 来源:网络整理
导读:我试图从web中读取 python模块中的一些数据. 我设法阅读,但在解析这些数据和获取所需信息方面遇到一些困难. 我的代码如下.任何帮助表示赞赏. #!/usr/bin/python2.7 -ttimport urllibimport urllib2def Connect2Web(): aResp = urllib2.urlopen("https://unis
我试图从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 = "<td><b>SILVER PASSBOOK ACCOUNT</b></td>" + "<td>(.*)</td>" * 4
  m = 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"

如果您在循环中进行匹配,请不要忘记重新编译模式.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读