python爬取所有A股个股代码与股名并保存为excle格式

11 篇文章 2 订阅

使用requests, lxml, xlwt库,完成任务

利用pip3 install 库名   安装各种第三方库

import requests
from lxml import etree
import xlwt

headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36"}


def get_stock():
    # 所有A股代码列表,返回股票信息列表
    url = "http://quote.eastmoney.com/stock_list.html"
    response = requests.get(url, headers=headers)
    response.encoding = "gbk"
    e = etree.HTML(response.text)
    stock_list = e.xpath( '//div[@class="quotebody"]//ul/li/a/text()')
    stock_url_list = e.xpath( '//div[@class="quotebody"]//ul/li/a/@href')
    #print(stock_list, stock_url_list)
    lists = []
    for i in range(0,len(stock_list)):
        stockid = stock_list[i].replace("(",",").replace(")","").split(",")[1].strip()
        stockname = stock_list[i].replace("(",",").replace(")","").split(",")[0].strip()
        stockurl = stock_url_list[i].strip()
        list_info = [stockid, stockname, stockurl]
        #print(list_info)
        lists.append(list_info)
    return lists


# 保存为excle格式
def excle_save(data_list):
    '''
    data_list 格式为: [[],[],[]...[]]
    '''
    book = xlwt.Workbook(encoding='utf-8', style_compression=0)
    sheet = book.add_sheet('mysheet', cell_overwrite_ok=True)
    # 循环插入数据(表头)
    raw = ['代码','公司名','个股网址']
    for i in range(len(raw)):
        sheet.write(0,i,raw[i])
    # 插入每个股票数据,是以一行一行的插入
    for i in range(len(data_list)):
        for j in range(len(data_list[i])):
            print(i + 1, j, data_list[i][j])
            sheet.write(i + 1, j, data_list[i][j])
    # 最后要保存
    book.save("stock_excle.xls")


if __name__ == "__main__":
    excle_save(get_stock())

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值