python正则匹配查询办理进度示例分享
投稿:zxhpj
[code]
import socket
import re
def gethtmlbyidentityid(identityid):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = 'www.gdcrj.com';
suburl = '/wsyw/tcustomer/tcustomer.do?&method=find&applyid={0}'
port = 80;
remote_ip = socket.gethostbyname(host)
s.connect((remote_ip , port))
print('【INFO】:socket连接成功')
message = 'GET '+ suburl.format(identityid) +' HTTP/1.1\r\nHost: '+ host +'\r\n\r\n'
# str 2 bytes
m_bytes = message.encode('utf-8')
# send bytes
s.sendall(m_bytes)
print('【INFO】:远程下载中...')
recevstr = ''
while True:
# return bytes
recev = s.recv(4096)
# bytes 2 str
recevstr += recev.decode(encoding = 'utf-8', errors = 'ignore')
if not recev:
s.close()
print('【INFO】:远程下载网页完成')
break
return recevstr
'''
利用正则表达式从上步获取的网页html内容里找出查询结果
'''
def getresultfromhtml(htmlstr):
linebreaks = re.compile(r'\n\s*')
space = re.compile('( )+')
resultReg = re.compile(r'\<td class="news_font"\>([^<td]+)\</td\>', re.MULTILINE)
#去除换行符和空格
htmlstr = linebreaks.sub('', htmlstr)
htmlstr = space.sub(' ', htmlstr)
#匹配出查询结果
result = resultReg.findall(htmlstr)
for res in result:
print(res.strip())