实时抓取雪球网-泸深-牛股的股票数据

分析:

进入雪球网>在行情>点击行情>在泸深股市>点击泸深一览>>>对这块的数据进行抓取

打开浏览器的抓包工具:

分析结论:

代码部分:

import requests,time
from prettytable import ALL
from prettytable import PrettyTable #使用prettytable 建立表格存入数据,并且以csv格式保存在本地

def get_requests(table_by_symbol):

    headers = {

        "Connection":'close',#不保持连接
        "User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.67 Safari/537.36 Edg/87.0.664.47',
    }#ua伪装
    params = {
        'page': '1',
        'size': '90',
        'order': 'desc',
        'orderby': 'percent',
        'order_by': 'percent',
        'market': 'CN',
        'type': 'sh_sz',
        '_': int(round(time.time()*1000)),
    }
    now_time = time.strftime("%m-%d %H:%M:%S")#当前时间
    xueqiu_url = 'https://xueqiu.com/service/v5/stock/screener/quote/list'
    session = requests.Session()
    session.get(url='https://xueqiu.com/',headers=headers)#cookie处理
    
    response = session.get(url=xueqiu_url,headers=headers,params=params)
    response.encoding = 'utf-8'
    json_list = response.json()['data']['list']#发起请求(这里只抓一页的股票数据)
   
    for dic in json_list:#数据解析提取
        if dic['chg'] > 2:
            name = dic['name']#股票名
            symbol = dic['symbol']#编号
            current = dic['current']#股票当前价
            chg = dic['chg']#涨跌额
            percent = dic['percent']#涨跌幅
            current_year_percent = dic['current_year_percent']#年初至今
            turnover_rate = dic['turnover_rate']#换手
            amplitude = dic['amplitude']#振幅
           

            table_by_symbol.add_row([now_time,symbol,name,current,chg,percent,current_year_percent,turnover_rate,amplitude])
        
    print(table_by_symbol)
    return True    

def ptable_to_csv(table,filename,headers=True):#保存数据
    
    raw = table.get_string()
    data = [tuple(filter(None,map(str.strip,splitline)))
            for line in raw.splitlines()
            for splitline in [line.split('|')] if len(splitline) > 1]
    if not headers:
        data = data[1:]
    with open (filename,'w',encoding='utf-8-sig') as fp:
        for i in data:
            fp.write('{}\n'.format(','.join(i)))
                      
def main():
    table_by_symbol = PrettyTable(['抓取数据时间','股票ID','股票名称','当前价','涨跌额','涨跌幅','年初至今','换手','振幅'])
    while True:#每隔30秒抓一次数据(打印出来并且保存在本地)
        get_requests(table_by_symbol)
        ptable_to_csv(table_by_symbol,'data.csv',headers=True)
        time.sleep(30)
                  
main()

ok,下面是跑该代码的的图片:

posted @ 2021-03-16 16:36  我是容易  阅读(157)  评论(0编辑  收藏  举报