python正则匹配查询港澳通行证办理进度示例分享_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】:按任意键退出')

时间: 2024-08-27 04:30:22

python正则匹配查询港澳通行证办理进度示例分享_python的相关文章

python实现猜数字游戏(无重复数字)示例分享_python

复制代码 代码如下: import time, random class GuessNum:    def __init__(self):        self._num = ''        self.input_num = []        self.count = 1                                      #猜对所用次数        self.sec = 0                                           #猜

python 正则匹配到特定字符串,并删除字符串所在的行,并且返回所在行的第一个数字ID

问题描述 python 正则匹配到特定字符串,并删除字符串所在的行,并且返回所在行的第一个数字ID 如利用正则匹配到"受到xx影响",则删除字符串所在行,并返回2877,2881.a.txt内容如下:2877 a3 1-1 9:16部分地区受到雾霾影响出行不便. 2878 a2 1-1 9:42床前明月光,疑是地上霜.2880 a2 1-1 10:09举头望明月,低头思故乡.2881 a3 1-1 9:16受到事故影响出行不便. 希望得到的结果是:"2877"匹配到

re 爬虫-python正则匹配有关汉字的问题

问题描述 python正则匹配有关汉字的问题 match=re.findall('''未来的人''',unicodePage,re.S). 怎样才能匹配,需要找到所有未来的人所有链接

python正则匹配结果太多

问题描述 python正则匹配结果太多 直接上代码 str = 'MBLNR = WA_DATA-BELNR AND ZEILE = WA_DATA-BUZEI AND MJAHR = WA_DATA-BUDAT+0(4)' reg = r'-(.+)[ ]|-(.+)$' m = re.findall(reg,str) print(m) 得到的结果是 [('BELNR', ''), ('BUZEI', ''), ('', 'BUDAT+0(4)')] 里面这么多空的字符串是什么意思?该怎么写

求大神帮我用c#正则匹配查询一下html页面的代码

问题描述 求大神帮我用c#正则匹配查询一下html页面的代码 <div class=""result c-container ************* </a></div>< 中间****号为要查找的内容 解决方案 有嵌套的div没有?有的话还不如字符串前后截取.没有可以用下面的 Regex rx = new Regex(""<div\s+class=""result c-container[^>

python正则匹配一个html中的几个url

问题描述 python正则匹配一个html中的几个url 在一个网页中匹配出如下的几个url,从url开始匹配不是从href开始匹配 href="http://redirect.wangpansou.cn/redirect.php?url=http%3A%2F%2Fpan.baidu.com%2Fshare%2Flink%3Fuk%3D2803502175%26shareid%3D3310887851%26third%3D0" href="http://redirect.wa

使用python实现正则匹配检索远端FTP目录下的文件_python

遇到一个问题,需要正则匹配远端FTP目录下的文件,如果使用ftp客户端可以通过命令行很容易的做到这一点,但是暂时没有一个工具支持这样的需求,于是通过python对FTP的支持和对正则表达式的支持,写了这么一个简单的工具,用于使用正则表达式来匹配远端目录的文件. 代码如下 # coding=utf-8 ######################################################################### # File Name: reg_url.py #

Python实现的一个自动售饮料程序代码分享_python

写这个程序的时候,我已学习Python将近有一百个小时,在CSDN上看到有人求助使用Python如何写一个自动售饮料的程序,我一想,试试写一个实用的售货程序.当然,只是实现基本功能,欢迎高手指点,新手学习参考. 运行环境:Python 2.7 # encoding=UTF-8 loop=True money=0 while loop:     x = raw_input('提示:请投入金币,结束投币请按"q"键')     if x=='q':         if money==0:

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

复制代码 代码如下: import urllib.requestimport reimport time def movie(movieTag):     tagUrl=urllib.request.urlopen(url)    tagUrl_read = tagUrl.read().decode('utf-8')    return tagUrl_read def subject(tagUrl_read):     '''         这里还存在问题:        ①这只针对单独的一页