每五分钟抓取网站上的A股数据-----练习

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import psycopg2
from multiprocessing import Process
import requests
import time

def get_g(page_mun,tm):
    url="https://xueqiu.com/service/v5/stock/screener/quote/list?page=%s&size=30&order=desc&orderby=percent&" \
        "order_by=percent&market=US&type=us&_="% page_mun
    headers = {'User-Agent': 'User-Agent:Mozilla/5.0'}
    a = requests.get(url + str(int(time.time() ))+"123", headers=headers, verify=False)
    connection = psycopg2.connect(database="cn_prices", user="postgres", password="111112", host="192.168.0.109",port="5432")
    cursor_pg = connection.cursor()
    for x in a.json()['data']['list']:
             #   prices=['时间','股票代码','股票名称','成交额',"成交量",'当前价格','涨跌']
            prices = [tm, str(x['symbol']).lower(),str(x['name']).lower(), str(x['amount']).lower(), str(x['volume']).lower(),
                      str(x['current']).lower(), str(x['percent']).lower()]
            print(prices)
            cursor_pg.execute("INSERT INTO data_cn (date,symbol,name,amount,volume,current,percent)  "
                              "VALUES (\'%s\', \'%s\', \'%s\', \'%s\', \'%s\',\'%s\',\'%s\' )"
                              %(prices[0],prices[1].lower(),prices[2].lower(),prices[3],prices[4],prices[5],prices[6]))
            connection.commit()
    cursor_pg.close()
    connection.close()

def get_all(start_page, stop_page):
    tm = time.strftime('%Y%m%d%H%M', time.localtime(time.time()))
    for i in range(start_page, stop_page+1):
        try:
            get_g(i, tm)
        except Exception as e:
             print(e)
             pass

if __name__ == '__main__':
    while True :
        p1 = Process(target=get_all, args=(1,124))
        p1.start()
        time.sleep(300)
    

猜你喜欢

转载自www.cnblogs.com/zhang---1/p/10908692.html