python每五分鍾抓取網站上的A股數據-----練習


 1 #!/usr/bin/env python
 2 # -*- coding: utf-8 -*-
 3 import os
 4 import sqlite3
 5 from multiprocessing import Process
 6 import requests
 7 import time
 8 
 9 def get_g(page_mun,tm):
10     url="https://xueqiu.com/service/v5/stock/screener/quote/list?page=%s&size=30&" \
11         "order=desc&orderby=percent&order_by=percent&market=CN&type=sh_sz&_="% page_mun
12     headers = {'User-Agent': 'User-Agent:Mozilla/5.0'}
13     a = requests.get(url + str(int(time.time() ))+"123", headers=headers, verify=False)
14     conn = sqlite3.connect('CN_prices.db' )
15     cursor = conn.cursor()
16     print(str(page_mun)+"")
17     for x in a.json()['data']['list']:
18             name=x['name']
19             prices = [tm, str(x['symbol']).lower(), name, str(x['amount']).lower(), str(x['volume']).lower(),
20                       str(x['current']).lower(), str(x['percent']).lower()]
21             print(prices)
22             cursor.execute('insert into data_cn(date,symbol,name,amount,volume,current,percent) values'
23                            ' (\'%s\',\'%s\',\'%s\',\'%s\',\'%s\',\'%s\',\'%s\')'%
24                            (prices[0],prices[1],prices[2],prices[3],prices[4],prices[5],prices[6]))
25             conn.commit()
26     cursor.close()
27     conn.close()
28 
29 def get_all(start_page, stop_page):
30     tm = time.strftime('%Y%m%d%H%M', time.localtime(time.time()))
31     for i in range(start_page, stop_page+1):
32         try:
33             get_g(i, tm)
34         except Exception as e:
35              print(e)
36              pass
37 
38 
39 if __name__ == '__main__':
40     if os.path.exists('CN_prices.db'):
41         pass
42     else:
43         conn = sqlite3.connect('CN_prices.db' )
44         cursor = conn.cursor()
45         cursor.execute('create table data_cn (date TEXT,symbol TEXT, name TEXT,amount TEXT,'
46                        'volume TEXT,current TEXT,percent TEXT)')
47         conn.commit()
48         cursor.close()
49         conn.close()
50     while True :
51         p1 = Process(target=get_all, args=(1, 20))
52         print('Process will start.')
53         p1.start()
54         time.sleep(300)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM