import requests import pymysql import json import time import re import os url = "https://mnks.jxedt.com/get_question?r=0.46514869467754005&index={}" db = pymysql.connect(host="10.10.10.100",port=3306, user="root", password="12341234", database="gofiber") cursor = db.cursor() TABLE = "car" def loads_str(data_str): try: result = json.loads(data_str) return result except Exception as e: error_index = re.findall(r"char (\d+)\)", str(e)) if error_index: error_str = data_str[int(error_index[0])] data_str = data_str.replace(error_str, "") # 该处将处理结果继续递归处理 return loads_str(data_str) for i in range(1, 10000): if i % 1000 == 0: time.sleep(3) # 请求链接 response = requests.get(url.format(i)) # 加载数据,过滤escape question_data = loads_str(response.text) try: if question_data["question"] != "": sql = "INSERT INTO %s (`id`, `question`, `options`, `image`, `answer`, `bestanswer`, `type`, `a`, `b`, `c`, `d`, `e`, `f`, `g`, `cid`) " % TABLE sql += "VALUES (%d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d );" % ( question_data["id"], question_data["question"], question_data["options"], question_data["imageurl"], question_data["ta"], question_data["bestanswer"], question_data["type"], question_data["a"], question_data["b"], question_data["c"], question_data["d"], question_data["e"], question_data["f"], question_data["g"], question_data["cid"], ) try: # 执行sql语句 cursor.execute(sql) # 提交到数据库执行 db.commit() except: continue else: continue except: print("index %d 存在错误", i) continue # 查询所有问题,解析,正确答案 # SELECT id, question, IF(answer = 1,a, IF(answer = 2,b,IF(answer = 3,c,d))) as answer,bestanswer,answer as answer_id from car WHERE a != "";