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】:按任意键退出')

以上就是python正则匹配查询港澳通行证办理进度示例分享。大多数人做事没有成功,根本的原因是缺少做的魄力,缺少奋力拼搏的勇气。魄力,沉睡的人体内,一旦被唤醒,会做出许多神奇的事情来。你不要受自筑藩篱或禁闭的思想影响,而放弃追寻理想的魄力。更多关于python正则匹配查询港澳通行证办理进度示例分享请关注haodaima.com其它相关文章!

您可能有感兴趣的文章
Python自动化运维-使用Python脚本监控华为AR路由器关键路由变化

Python自动化运维-netmiko模块设备自动发现

Python自动化运维—netmiko模块连接并配置华为交换机

Python自动化运维-利用Python-netmiko模块备份设备配置

Python自动化运维-Paramiko模块和堡垒机实战