Python regular expression matching query Progress of Hong Kong and Macao passes

Source: Internet
Author: User
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 ')

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.