1 import requests
2 import json
3 import pymysql
4
5 class mysql_conn(object):
6 # 魔术方法, 初始化, 构造函数
7 def __init__(self):
8 self.db = pymysql.connect(host='127.0.0.1', user='root', password='abc123', port=3306, database='py1011')
9 self.cursor = self.db.cursor()
10 # 执行modify(修改)相关的操作
11 def execute_modify_mysql(self, sql):
12 self.cursor.execute(sql)
13 self.db.commit()
14 # 魔术方法, 析构化 ,析构函数
15 def __del__(self):
16 self.cursor.close()
17 self.db.close()
18
19
20 headers = {
21 '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',
22 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36',
23 }
24 url = 'https://xueqiu.com/v4/statuses/public_timeline_by_category.json?since_id=-1&max_id=-1&count=10&category=111'
25
26 response = requests.get(url,headers=headers)
27
28 res_dict = json.loads(response.text)
29
30 list_lsit = res_dict['list']
31
32 db ={}
33 for list_item_dict in list_lsit:
34 data_dict = json.loads(list_item_dict['data'])
35
36 db['id'] = data_dict['id']
37 db['title'] = data_dict['title']
38 db['description'] = data_dict['description']
39 db['target'] = data_dict['target']
40 try:
41 sql = 'insert into xueqiu (uid,title,description,target) values ("{id}","{title}","{description}","{traget}")'.fromart(**db)
42 mc = mysql_conn()
43 mc.execute_modify_mysql(sql)
44 except:
45 pass