美文网首页
python 中获取网络数据

python 中获取网络数据

作者: 江魁 | 来源:发表于2017-11-27 12:18 被阅读0次

r = urllib.urlopen('http://www.yunzhongmoke.com/MrJiang_URL.html')

使用正则表达式爬取网页

py2

import urllib2

import re

dStr = urllib2.urlopen('https://hk.finance.yahoo.com/q/cp?s=%5EDJI').read()

m = re.findall('(.*?)(.*?).*?(.*?).*?', dStr)

if m:

print m

print '\n'

print len(m)

else:

print 'not match’

py3

import urllib.request

import re

dStr = urllib.request.urlopen('https://hk.finance.yahoo.com/q/cp?s=%5EDJI').read()

getdStr=dStr.decode()

#在python 3中urllib.read()返回bytes对象而非str,语句功能是将dStr转换成str

#convert dStr into str, urllib.read() returns bytes objects instead of str

m = re.findall('(.*?)(.*?).*?(.*?).*?', getdStr)

if m:

print(m)

print('\n')

print (len(m))

else:

print ('not match')

相关文章

网友评论

      本文标题:python 中获取网络数据

      本文链接:https://www.haomeiwen.com/subject/fegrbxtx.html