Python code query The progress of Hong Kong-Macau Pass

Source: Internet
Author: User

Check the Python 3.3 code for the progress query for the Hong Kong and Macau Pass. Use the socket to request the relevant website, get the results and use the regular to find out the progress of processing. In fact, with URLLIB code will be more concise, but at that time is not familiar with urllib~

Directly on the code:

ImportSocketImportRe" "Guangdong Provincial Public Security Bureau entry-Exit government service network passport, pass processing progress inquiry. Parse URL format for http://www.gdcrj.com/wsyw/tcustomer/tcustomer.do?&method=find&applyid= ID number construct socket request Web page HTML, Match the query results with regular matches" "defGethtmlbyidentityid (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 connection succeeded') Message='GET'+ Suburl.format (Identityid) +'Http/1.1\r\nhost:'+ Host +'\r\n\r\n'     #Str 2 bytesM_bytes = Message.encode ('Utf-8')     #Send bytesS.sendall (m_bytes)Print('"INFO": Remote download ...') Recevstr="'     whileTrue:#Return bytesRecev = S.RECV (4096)        #bytes 2 StrRecevstr + = recev.decode (encoding ='Utf-8', errors ='Ignore')        if  notrecev:s.close ()Print('"INFO": Remote download page Complete')             Break    returnRecevstr" "use regular expressions to find the query results from the HTML content of the Web page you get from the previous step" "defgetresultfromhtml (HTMLSTR): Linebreaks= Re.compile (r'\n\s*') Space= Re.compile ('( )+') Resultreg= Re.compile (r'\<td class= "News_font" \> ([^<td]+] \</td\>', Re. MULTILINE)#remove newline characters and spacesHtmlstr = Linebreaks.sub ("', htmlstr) htmlstr= Space.sub (' ', Htmlstr)#match the results of the queryresult =Resultreg.findall (HTMLSTR) forResinchResult:Print(Res.strip ())if __name__=='__main__': Identityid= Input ('Enter your ID number (only for residents in Guangdong province):')    Try: Identityid=Int (Identityid)Print('"INFO": Start query') HTML=Gethtmlbyidentityid (Identityid) getresultfromhtml (HTML)Print('"INFO": Query succeeded')    except:        Print('"WARN": Enter illegal') input ('"INFO": Press any key to exit')

Python code query The progress of Hong Kong-Macau Pass

Related Article

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.