您当前的位置:首页 > 计算机 > 编程开发 > Python

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

时间:11-15来源:作者:点击数:

使用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())

 

方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门