This is the first day of my participation in the Gwen Challenge in November. Check out the details: the last Gwen Challenge in 2021

A coder who doesn’t want to invest is not a good cook.

1 why write this article

I had just learned a little about Python and felt it was a great language and tool to use to expand my skills and knowledge and to play around in a new world. This is a series of articles. From here on, I will share my own learning experience and finally get the fun of learning and explore the method of fund investment. I can not only learn knowledge but also earn a little money to buy vegetables.

2 Where to get the data

This is ready for you,

# to open the connection, can see the corresponding funds to find information on http://fund.eastmoney.com/jzzzl.htmlCopy the code

With fund connection, what we need to do is how to grab it down. At the beginning of 123, I happened to find the interface of background access, isn’t it amazing? It’s time for the picture above, you can see the picture below:

# this is the original connection, http://fund.eastmoney.com/Data/Fund_JJJZ_Data.aspx?lx=1&sort=zdf desc&page = 2200 & onlySale = 0 # after I use the postman I can't help but laugh at the fact that parameter passing is spelled with Chinese initials, # lx, which is clearly a shorthand for type. Sort means that sorting certain fields can be ignored. For pages 2,200 would be the second page,200 per page, and onlySale would be the condition to sell. http://fund.eastmoney.com/Data/Fund_JJJZ_Data.aspx?lx=1&sort=zdf,desc&page=2,100&onlySale=0Copy the code

3. How to capture data

Here we use python, need to install the libraries have requests/demjson prettytable/json, have very simple?

PIP install demjson # PIP install prettytable # PIP install demjson # PIP install demjson # PIP install prettytable # PIP install demjson # PIP install demjson # PIP install demjson # Convert jSON-formatted text to JSON PIP install JSONCopy the code

Here I can go to the code. The simple logic is to grab the information returned by the API interface, then parse the message, convert the returned result into JSON format, select only the content to be obtained, and finally output the obtained result.

import requests
import json
import demjson
from prettytable import PrettyTable

The table header field of the data table
title_list = ["code"."name"."value"]

# Query fund list information
def query_fund_list(page= 1) :
    req_url = "http://fund.eastmoney.com/Data/Fund_JJJZ_Data.aspx?lx=1&sort=zdf,desc&page={},20&onlySale=0".format(page)
    response = requests.get(req_url)
    Output the response header
    # print(response.headers)
    Get the request result and replace it, otherwise the result cannot be formatted json
    resp_body = response.text.replace("var db="."")
    # We would have preferred this json because JSON does not support transformations like {a :"1"}, so we used Demojson instead
    # json_data = json.loads(resp_body)
    Convert objects to JSON objects, format irregular JSON objects to JSON objects
    resp_body = demjson.decode(resp_body)
    Get the result array
    fund_list = resp_body["datas"]
    body_list = []
    for node in fund_list:
        tmp = []
        tmp.append(node[0])
        tmp.append(node[1])
        tmp.append(node[3])
        body_list.append(tmp)
    Create an object PrettyTable to print the output
    bt = PrettyTable()
    Put header information into BT
    bt.field_names = title_list
    # Place the table contents in BT
    bt.add_rows(body_list)
    Print the result
    print(bt)

if __name__ == "__main__":
    # here is only the first page printed, circular print results do not write, everyone can
    query_fund_list(1)

Copy the code

The final output is shown in the figure

The end result is this, and with these results we can store them structurally, query them in the database, and use them.

The fund code Name of the fund The latest net
005585 Galaxy entertainment mix 1.6363
001907 Investment UBS border xuan flexible configuration mixed A 3.3055
001908 Investment UBS border xuan flexible configuration mixed C 3.1667
164818 Icbc China Media Index (LOF)A 0.9231
. . .

This is an easy start. We get a list of funds. Subsequently, we will capture the basic information and change information of the fund and build a model to show it.