Share The original python3.3 code for querying the progress of the Hong Kong and Macao passes. Use socket requests to relevant websites, obtain results, and use regular expressions to find handling progress
The code is as follows:
Import socket
Import re
'''
Guangdong provincial authority entry-exit government affairs service network passport, pass handling progress query.
The analysis URL is formatted as http://www.gdcrj.com/wsyw/tcustomer/tcustomer.do? & Method = find & applyid = ID card number
Construct the html of the socket request webpage and obtain the query result using regular expression matching.
'''
Def gethtmlbyidentityid (identityid ):
S = socket. socket (socket. AF_INET, socket. SOCK_STREAM)
Host = 'www .gdcrj.com ';
Suburl = '/wsyw/tcustomer. do? & Method = find & applyid = {0 }'
Port = 80;
Remote_ip = socket. gethostbyname (host)
S. connect (remote_ip, port ))
Print ('[INFO]: socket connection successful ')
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]: remote download ...')
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]: the web page is downloaded remotely ')
Break
Return recevstr
'''
Use the regular expression to find the query result from the html content of the webpage obtained in the previous step.
'''
Def getresultfromhtml (htmlstr ):
Linebreaks = re. compile (r' \ n \ s *')
Space = re. compile ('() + ')
Resulreg = re. compile (r '\([^ ', Re. MULTILINE)
# Remove line breaks and spaces
Htmlstr = linebreaks. sub ('', htmlstr)
Htmlstr = space. sub ('', htmlstr)
# Matching query results
Result = resulregulatory. findall (htmlstr)
For res in result:
Print (res. strip ())
If _ name _ = '_ main __':
Identityid = input ('Enter your ID number (only for Guangdong residents ):')
Try:
Identityid = int (identityid)
Print ('info]: start query ')
Html = gethtmlbyidentityid (identityid)
Getresultfromhtml (html)
Print ('[INFO]: query succeeded ')
Except t:
Print ('[WARN]: illegal input ')
Input ('[INFO]: press any key to exit ')