雪球美股

import requests
import csv
from pyecharts import Bar

url = 'https://xueqiu.com/hq?page=1#exchange=US&firstName=3&secondName=3_0'
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36"
}
session = requests.Session()
session.get(url=url,headers=headers)


ALL_DATA = []
def get_page_list():
    url = 'https://xueqiu.com/service/v5/stock/screener/quote/list'
    for i in range(1,254):
        params = {
            'page': i,
            'size': '30',
            'order': 'desc',
            'orderby': 'percent',
            'order_by': 'percent',
            'market': 'US',
            'type': 'us',
            '_': '1561292727168'
        }

        response = session.get(url=url, headers=headers, params=params)
        page_text = response.json()  # dict
        # print(page_text)
        content_list = page_text['data']['list']
        # print(content_list)

        for stock in content_list:
            info_dict = {}
            stock_code = stock['symbol']
            stock_name = stock['name']
            cur_price = stock['current']
            zhangdie = stock['percent']
            ttm = stock['pe_ttm']
            value = stock['market_capital']
            if value:
                value = value/10000


            info_dict['股票代码'] = stock_code
            info_dict['股票名称'] = stock_name
            info_dict['当前价'] = cur_price
            # info_dict['涨跌幅'] = float(str(zhangdie)+'%')
            info_dict['涨跌幅(%)'] = zhangdie
            info_dict['市值'] = str(value)+'万'
            info_dict['市盈率'] = ttm

            # print(info_dict)
            ALL_DATA.append(info_dict)
    # print(ALL_DATA)


def main():
    get_page_list()
    try:
        ALL_DATA.sort(key=lambda data: data["涨跌幅(%)"],reverse=True)
        print(ALL_DATA)
        data = ALL_DATA[0:100]

        # 图形化展示
        stock_name = list(map(lambda x:x['股票名称'], data))
        zhangdie = list(map(lambda x:x['涨跌幅(%)'], data))
        chart = Bar()
        chart.add("涨幅最大的100个股票",
                  stock_name,
                  zhangdie,
                  is_more_utils=True)
        chart.render('stock.html')


        # 保存到csv
        with open('./雪球美股.csv', 'w', encoding='utf-8', errors='ignore', newline="") as csvfile:
            fieldnames = ['股票代码', '股票名称', '当前价', '涨跌幅(%)', '市值', '市盈率']
            writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
            writer.writeheader()
            writer.writerows(data)
    except:
        pass

if __name__ == '__main__':
    main()

 

上一篇:Python设计模式之命令模式


下一篇:Mysql的JOIN