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'\
#去除换行符和空格
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】:按任意键退出')

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

Regular expressions are powerful tools for pattern matching and text manipulation in programming, enhancing efficiency in text processing across various applications.

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

In Python, how to dynamically create an object through a string and call its methods? This is a common programming requirement, especially if it needs to be configured or run...

The article discusses popular Python libraries like NumPy, Pandas, Matplotlib, Scikit-learn, TensorFlow, Django, Flask, and Requests, detailing their uses in scientific computing, data analysis, visualization, machine learning, web development, and H
