How to capture administrative area code using Python

Source: Internet
Author: User
The code of various administrative divisions is required for project implementation. recently, this requirement happened, so I searched the internet and shared it with you after testing, this article will share with you the sample code for capturing administrative code using Python. if you need it, you can refer to it for reference. let's study it with Xiaobian. Preface

The National Bureau of Statistics website has relatively consistent Administrative Code. for some websites, this is a very basic data, so I wrote a Python program to capture this part of the data.

Note:After capturing it, you need to organize it manually.

Sample code:

#-*-Coding: UTF-8-*-''' obtain the administrative division code ''' import requests, rebase_url = 'http: // javasdef get_xzqh (): html_data = requests. get (base_url ). content pattern = re. compile ('

(\ D + ).*? (.*?)

') Areas = re. findall (pattern, html_data) print "code, name, level" for area in areas: print area [0], area [1]. decode ('utf-8 '). replace (u'', ''), area [1]. decode ('utf-8 '). count (u'') if _ name __= = '_ main _': get_xzqh ()

Note:

In addition, there is another channel for obtaining information about the country/region table, that is, the country/region information table that comes with the QQ software. (The file name isLocList.xml), The general storage location is:C:\Program Files\Tencent\QQ\I18N\2052

If you need to install the Chinese version of QQ, you can obtain it. if you need the English version, install the English version of QQ. The international version is in the 1033 directory.

The code is written according to the iso00006 standard and can be easily imported into the database.

Summary

The above is the use of Python to get all the content of the administrative division code. I hope this article will be helpful for you to learn or use python. if you have any questions, you can leave a message.

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.