Python Crawler App version JSON parsing example

Source: Internet
Author: User

One: We go through Charles to grab a piece of app request, return the JSON data from the server, and beautify the JSON code through the Chrome plugin Jsonview as shown


We want to grab ID and name


2: Write our code in the PYCHARM environment

# -*- coding: utf-8 -*-  __author__ =  ' Itiwin ' import requests import  json import os import sys import time import traceback import MySQLdb-Reload (SYS) SY S.setdefaultencoding ("Utf-8")   conn= mysqldb.connect (host= ' localhost ', port = 3306, user= ') Root ', passwd= ' root ', db = ' Db_expert ', conn.set_character_set (' UTF8 ') cur = conn.cursor () Cur.execu

Te (' set names utf8; ')

Cur.execute (' set character set utf8; ')

Cur.execute (' set character_set_connection=utf8; ') Cur.close ()   headers = {' Accept ':  ' text/html,application/xhtml+xml,application/xml;q=0.9,*/ *;q=0.8 ', ' accept-encoding ':  ' gzip, deflate, compress ', ' accept-language ':  ' =0.3 ', ' Cache-control ':  ' max-age=0 ', ' Connection ':  ' keep-alive ', ' user-agent ':  ' ( WINDOWS NT 6.1; WOW64) AppleWebKit/537.36  (Khtml, like gecko)  chrome/43.0.2357.65 safari/537.36 '}

Session=requests.session ();

Session.headers.update (Headers) Def getnav ():     url =  ' http://get json url '     r = requeturl (URL)     j = r.text      jn = json.loads (j)     data = jn[' data ']      try:         print (U ' A total of several categories: ' +str (len (data))          for d in data:              id = d[' id ']              print ( d[' name ')              Getmatchinfo (ID)     except:       &nbsP; traceback.print_exc (file=sys.stdout)     finally:          conn.close ()         session.close ()   if __ name__ == ' __main__ ':     getnav ()

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.