Def save_funds(): all_funds = target_util._get_all_funds() with open("fund.csv", 'a+') as f: F.rite (" fund code, Fund name, market, Category, type \n") for fund in all_funds: f.write("{fund[id]},{fund[name]},{fund[category]},{fund[tag]},{fund[type]}\n".format( fund=fund )) Logging. Info (" All fund information written!" ) if __name__ == "__main__": save_funds()Copy the code
Def _get_all_funds(): base_url = "http://fund.eastmoney.com/js/fundcode_search.js" resp = requests.get(base_url) if not resp.ok: Logging. error(" all fund information request failed: {0}".format(base_URL)) resp_str = resp.text. Replace ("var r = ", "").replace(";"). ,"") funds = ast.literal_eval(resp_str) all_funds = [] for fund in funds: # info = ast.literal_eval(fund) all_funds.append({"id": fund[0], "name": fund[2], "category": fund ", "tag": Fund [3], "type": "fund"}) logging. Warning (" all fund information is {0} ") .format(len(all_funds))) return all_fundsCopy the code