有大神告诉我为什么pymysql导入失败

import json
import requests
import pymysql


url = 'https://xueqiu.com/v4/statuses/public_timeline_by_category.json?since_id=-1&max_id=-1&count=10&category=111'

headers = {
'Cookie': 'aliyungf_tc=AQAAALoQF3p02gsAUhVFebQ3uBBNZn+H; xq_a_token=584d0cf8d5a5a9809761f2244d8d272bac729ed4; xq_a_token.sig=x0gT9jm6qnwd-ddLu66T3A8KiVA; xq_r_token=98f278457fc4e1e5eb0846e36a7296e642b8138a; xq_r_token.sig=2Uxv_DgYTcCjz7qx4j570JpNHIs; _ga=GA1.2.516718356.1534295265; _gid=GA1.2.1050085592.1534295265; u=301534295266356; device_id=f5c21e143ce8060c74a2de7cbcddf0b8; Hm_lvt_1db88642e346389874251b5a1eded6e3=1534295265,1534295722; Hm_lpvt_1db88642e346389874251b5a1eded6e3=1534295722',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36',

}

while True:
    response = requests.get(url,headers=headers)

    res_dict = json.loads(response.text)
    next_id = res_dict['next_max_id']
    next_url = 'https://xueqiu.com/v4/statuses/public_timeline_by_category.json?since_id=-1&max_id='+str(next_id)+'&count=10&category=111'
    url = next_url

    list_list = res_dict['list']


    for list_item in list_list:

        data_str = list_item['data']
        data_str = json.loads(data_str)
        s_id = data_str['id']
        s_title = data_str['title']
        s_description = data_str['description']
        s_target = data_str['target']




        #写入mysql
        db = pymysql.connect(host='127.0.0.1',user='root',password='123456',port='3306',database='xueqiu',charset='utf-8')
        cursor = db.cursor()
        sql = "insert into iceball values (null ,'{}','{}','{}','{}')".format(s_id,s_title,s_description,s_target)

        cursor.execute(sql)
        db.commit()
        cursor.close()
        db.close()

        # 写入文件版本
        # temp = str(s_id) + ' ' + str(s_title) + ' ' + str(s_description) + ' ' + s_target
        # print(temp)
        # with open('xinwen.html','a',encoding='utf-8') as f:
        #     f.write(temp + '\n')

猜你喜欢

转载自blog.csdn.net/majiexiong/article/details/81712169