Home Backend Development Python Tutorial In-depth analysis of python data mining Json structure analysis

In-depth analysis of python data mining Json structure analysis

Apr 21, 2018 pm 02:26 PM
javascript json python

This article analyzes and summarizes the relevant knowledge points of Python data mining and Json structure analysis through examples. Friends who are interested in this can refer to it.

json is a lightweight data exchange format, which can also be said to be a configuration file format

Files in this format are what we often encounter in data processing

Python provides a built-in module json, which only needs to be imported before use

You can view the json help document through the help function

The commonly used methods of json include load, loads, dump and dumps. These are all beginners in python and I will not do it. Too many explanations

json can be used in conjunction with a database, which is very useful when processing large amounts of data in the future

Now we will formally use data mining to process json files

Many websites now use Ajax, so generally many of them are XHR files

Here I want to use a map website to demonstrate

We use the browser The debugging obtained the relevant url

https://ditu.amap.com/service/poiInfo?id=B001B0IZY1&query_type=IDQ

Next we simulate the browser through the get method in the requests module Issue an http request and return the result object

The code is as follows

# coding=utf-8
__Author__ = "susmote"

import requests
url = "https://ditu.amap.com/service/poiInfo?id=B001B0IZY1&query_type=IDQ"

resp = requests.get(url)
print(resp.text[0:200])
Copy after login

The result of running in the terminal is as follows

The data has been obtained, but in order to use the data next, we need to use the json module to analyze the data

The code is as follows

import requests
import json

url = "https://ditu.amap.com/service/poiInfo?id=B001B0IZY1&query_type=IDQ"

resp = requests.get(url)

json_dict = json.loads(resp.text)

print(type(json_dict))

print(json_dict.keys())
Copy after login

Let’s briefly talk about the above code:

Import the json module, then call the loads method, and pass the returned text as the parameter of the method

In The running result in the terminal is as follows

It can be seen that the result of the conversion is a dictionary corresponding to the json string, because type (json_dict) returns

Because the object is a dictionary, we can call the dictionary method. Here we call the keys method

The result returns three keys, namely status, searcOpt, data

Let’s check the data in the data key

import requests
import json

url = "https://ditu.amap.com/service/poiInfo?id=B001B0IZY1&query_type=IDQ"

resp = requests.get(url)

json_dict = json.loads(resp.text)

print(json_dict['data'])
Copy after login

Run this code in the terminal

You can see that there is a lot of data we need, such as

, which are not marked one by one. By comparing with what is displayed on the web page, we can know which ones It’s useful

Let’s get useful information through the code and output it clearly

# coding=utf-8
__Author__ = "susmote"

import requests
import json

url = "https://ditu.amap.com/service/poiInfo?id=B001B0IZY1&query_type=IDQ"

resp = requests.get(url)

json_dict = json.loads(resp.text)

data_dict = json_dict['data']

data_list = data_dict['poi_list']

dis_data = data_list[0]

print('城市: ', dis_data['cityname'])
print('名称: ', dis_data['name'])
print('电话: ', dis_data['tel'])
print('区号: ', dis_data['areacode'])
print('地址: ', dis_data['address'])
print('经度: ', dis_data['longitude'])
print('纬度: ', dis_data['latitude'])
Copy after login


Because what is returned is a dictionary, through the study of the file structure, the dictionary is nested with lists, and the lists are nested with dictionaries. Through layer-by-layer unnesting, the data is successfully obtained.

Here are the steps Listed separately, so you will see it more clearly

Let’s run the program through the terminal to get the information we want

Isn’t it very simple? Yes, this program can be used as a template. When obtaining information from other places, you only need to change a URL.

For example, the following examples

Peking University

Or Tencent Building

Data mining is endless. I hope everyone can analyze the data more and find the data you want.

Related recommendations:

How to process Python data numpy.median

##

The above is the detailed content of In-depth analysis of python data mining Json structure analysis. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Can the Python interpreter be deleted in Linux system? Can the Python interpreter be deleted in Linux system? Apr 02, 2025 am 07:00 AM

Regarding the problem of removing the Python interpreter that comes with Linux systems, many Linux distributions will preinstall the Python interpreter when installed, and it does not use the package manager...

How to solve the problem of Pylance type detection of custom decorators in Python? How to solve the problem of Pylance type detection of custom decorators in Python? Apr 02, 2025 am 06:42 AM

Pylance type detection problem solution when using custom decorator In Python programming, decorator is a powerful tool that can be used to add rows...

How to solve permission issues when using python --version command in Linux terminal? How to solve permission issues when using python --version command in Linux terminal? Apr 02, 2025 am 06:36 AM

Using python in Linux terminal...

Python 3.6 loading pickle file error ModuleNotFoundError: What should I do if I load pickle file '__builtin__'? Python 3.6 loading pickle file error ModuleNotFoundError: What should I do if I load pickle file '__builtin__'? Apr 02, 2025 am 06:27 AM

Loading pickle file in Python 3.6 environment error: ModuleNotFoundError:Nomodulenamed...

Do FastAPI and aiohttp share the same global event loop? Do FastAPI and aiohttp share the same global event loop? Apr 02, 2025 am 06:12 AM

Compatibility issues between Python asynchronous libraries In Python, asynchronous programming has become the process of high concurrency and I/O...

What should I do if the '__builtin__' module is not found when loading the Pickle file in Python 3.6? What should I do if the '__builtin__' module is not found when loading the Pickle file in Python 3.6? Apr 02, 2025 am 07:12 AM

Error loading Pickle file in Python 3.6 environment: ModuleNotFoundError:Nomodulenamed...

How to ensure that the child process also terminates after killing the parent process via signal in Python? How to ensure that the child process also terminates after killing the parent process via signal in Python? Apr 02, 2025 am 06:39 AM

The problem and solution of the child process continuing to run when using signals to kill the parent process. In Python programming, after killing the parent process through signals, the child process still...

See all articles