python正则匹配查询港澳通行证办理进度示例分享

yipeiwu_com6年前Python基础

复制代码 代码如下:

import socket
import re

'''
广东省公安厅出入境政务服务网护照,通行证办理进度查询。
分析网址格式为 http://www.gdcrj.com/wsyw/tcustomer/tcustomer.do?&method=find&applyid=身份证号码
构造socket请求网页html,利用正则匹配出查询结果
'''
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())

if __name__ == '__main__':
 identityid = input('输入您的身份证号码(仅限广东省居民查询):')
 try:
  identityid = int(identityid)
  print('【INFO】:开始查询')
  html = gethtmlbyidentityid(identityid)
  getresultfromhtml(html)
  print('【INFO】:查询成功')
 except:
  print('【WARN】:输入非法')

 input('【INFO】:按任意键退出')

相关文章

tensorflow使用神经网络实现mnist分类

本文实例为大家分享了tensorflow神经网络实现mnist分类的具体代码,供大家参考,具体内容如下 只有两层的神经网络,直接上代码 #引入包 import tensorflow...

各个系统下的Python解释器相关安装方法

Python下载 Python最新源码,二进制文档,新闻资讯等可以在Python的官网查看到: Python官网:http://www.python.org/ 你可以在一下链接中下载Py...

Python中使用Tkinter模块创建GUI程序实例

使用Tkinter模块来创建简单的GUI程序。 Tkinter的Widgets有:Button、Canvas、Checkbutton、Entry、Frame、Label、Listbox、...

使用PM2+nginx部署python项目的方法示例

之前面我们使用uwsgi部署项目比较繁琐,本章节介绍使用pm2+nginx一键部署django项目 PM2的主要特性: 内建负载均衡(使用Node cluster 集群模块)...

Python多线程及其基本使用方法实例分析

本文实例讲述了Python多线程及其基本使用方法。分享给大家供大家参考,具体如下: 学习Python的多线程(Multi-threading),至少应该要有进程与线程的基本概念,可以参考...