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

电脑资料 时间:2019-01-01 我要投稿
【www.unjs.com - 电脑资料】

   

    复制代码代码如下:

    import socket

    import re

    '''

    广东省公安厅出入境政务服务网护照,通行证办理进度查询,

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

    分析网址格式为 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'\([^', 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】:按任意键退出')

   

您可能感兴趣的文章:

python正则匹配抓取豆瓣电影链接和评论代码分享

python正则表达式去掉数字中的逗号(python正则匹配逗号)

    QQ空间 搜狐微博 人人网 开心网 百度搜藏更多

    Tags:python正则匹配 港澳通行证办理进度

    复制链接收藏本文打印本文关闭本文返回首页

    上一篇:python模拟登录百度代码分享(获取百度贴吧等级)

    下一篇:windows下wxPython开发环境安装与配置方法

   

相关文章

2014-03-03Python基本数据类型详细介绍

2014-05-05python读取浮点数和读取文本文件示例

2012-05-05ssh批量登录并执行命令的python实现代码

2014-06-06python使用正则表达式检测密码强度源码分享

2013-11-11讲解python参数和作用域的使用

2013-11-11python条件和循环的使用方法

2013-09-09python pickle 和 shelve模块的用法

2014-04-04python网络编程之TCP通信实例和socketserver框架使用例子

2013-11-11python列表与元组详解实例

2013-12-12天翼开放平台免费短信验证码接口使用实例

   

文章评论

   

最 近 更 新

   

python解析中国天气网的天气数据

Python httplib,smtplib使用方法

python读写文件操作示例程序

python登录QQ邮箱发信的实现代码

python操作MongoDB基础知识

Python yield使用方法示例

python处理json数据中的中文

python列表操作使用示例分享

Python 条件判断的缩写方法

python从ftp下载数据保存实例

   

热 点 排 行

   

Python入门教程 超详细1小时学会

python 中文乱码问题深入分析

比较详细Python正则表达式操作指

Python字符串的encode与decode研

Python open读写文件实现脚本

Python enumerate遍历数组示例应

Python 深入理解yield

Python+Django在windows下的开发

python 文件和路径操作函数小结

python 字符串split的用法分享

最新文章