搜尋
首頁後端開發Python教學python根據股票代碼取得當前數據

上班時間透過瀏覽器打開股票網站怕會被別人看到,沒關係,試試在命令列下執行程式碼看數據就行了。
輸入sh,就可以查看到上證指數
輸入sz,就可以查看深圳指數
輸入cyb,就可以查看創業板指數
其他的股票代碼可以自己自定義,加入到字典中就行了

python版本2.7 .3

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import urllib2
import re
import datetime
 
def getStockInfo(url):
    """根据url获取信息"""
    stockList = []
    request = urllib2.Request(url)
    response = urllib2.urlopen(request)
 
    stockStr = response.read()
    stockList = stockStr.split(',')
    return stockList
 
def printStock(List):
    """打印相关信息"""
    print '***********price*****************' + List[1]
    print '***********float_price***********' + List[2]
    print '***********float_perct***********' + List[3] + '%'
    print '***********succ_unit*************' + List[4]+' shou'
    print '***********succ_price************' + List[5]
 
def getUrlByCode(code):
    """根据代码获取详细的url"""
    url = ''
    stockCode = ''
    if code == 'sh':
        url = 'http://hq.sinajs.cn/list=s_sh000001'
    elif code == 'sz':
        url = 'http://hq.sinajs.cn/list=s_sz399001'
    elif code == 'cyb':
        url = 'http://hq.sinajs.cn/list=s_sz399006'
    else:
        pattern = re.compile(r'^60*')
        match = pattern.match(code)
        if match:
            stockCode = 'sh'+ code
        else:
            stockCode = 'sz' + code
        url = 'http://hq.sinajs.cn/list=s_'+stockCode
 
    return url
 
 
#输入stock代码输出对应的价格信息
#code = raw_input('code: ')
codeDict = {
    'sh'     : 'shang hai zq',
    'sz'     : 'shen zheng zq',
    'cyb'    : 'chang ye ban',
    '601788' : 'guang da zheng quan',
    '000651' : 'ge li dian qi',
}
 
#http://hq.sinajs.cn/list=s_sh000001 (上海大盘查询)
#http://hq.sinajs.cn/list=s_sz399001 (深圳大盘查询)
 
count = 0;
while (count<=100):#循环100次后再退出
    # 循环字典
    for key in codeDict:
        print key + &#39;--&#39;+codeDict[key]
 
    code = raw_input(&#39;please select a code: &#39;)
    now_time = datetime.datetime.now()
 
    #打印该code的信息
    url = getUrlByCode(code)
    stockInfo = getStockInfo(url)
    #print stockInfo
    printStock(stockInfo)
 
    end_time = datetime.datetime.now()
    costTime =  (end_time - now_time).seconds
    print &#39;总共花费时间&#39;+str(costTime)+&#39;秒&#39;
    count +=1


陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
列表和陣列之間的選擇如何影響涉及大型數據集的Python應用程序的整體性能?列表和陣列之間的選擇如何影響涉及大型數據集的Python應用程序的整體性能?May 03, 2025 am 12:11 AM

ForhandlinglargedatasetsinPython,useNumPyarraysforbetterperformance.1)NumPyarraysarememory-efficientandfasterfornumericaloperations.2)Avoidunnecessarytypeconversions.3)Leveragevectorizationforreducedtimecomplexity.4)Managememoryusagewithefficientdata

說明如何將內存分配給Python中的列表與數組。說明如何將內存分配給Python中的列表與數組。May 03, 2025 am 12:10 AM

Inpython,ListSusedynamicMemoryAllocationWithOver-Asalose,而alenumpyArraySallaySallocateFixedMemory.1)listssallocatemoremoremoremorythanneededinentientary上,respizeTized.2)numpyarsallaysallaysallocateAllocateAllocateAlcocateExactMemoryForements,OfferingPrediCtableSageButlessemageButlesseflextlessibility。

您如何在Python數組中指定元素的數據類型?您如何在Python數組中指定元素的數據類型?May 03, 2025 am 12:06 AM

Inpython,YouCansspecthedatatAtatatPeyFelemereModeRernSpant.1)Usenpynernrump.1)Usenpynyp.dloatp.dloatp.ploatm64,formor professisconsiscontrolatatypes。

什麼是Numpy,為什麼對於Python中的數值計算很重要?什麼是Numpy,為什麼對於Python中的數值計算很重要?May 03, 2025 am 12:03 AM

NumPyisessentialfornumericalcomputinginPythonduetoitsspeed,memoryefficiency,andcomprehensivemathematicalfunctions.1)It'sfastbecauseitperformsoperationsinC.2)NumPyarraysaremorememory-efficientthanPythonlists.3)Itoffersawiderangeofmathematicaloperation

討論'連續內存分配”的概念及其對數組的重要性。討論'連續內存分配”的概念及其對數組的重要性。May 03, 2025 am 12:01 AM

Contiguousmemoryallocationiscrucialforarraysbecauseitallowsforefficientandfastelementaccess.1)Itenablesconstanttimeaccess,O(1),duetodirectaddresscalculation.2)Itimprovescacheefficiencybyallowingmultipleelementfetchespercacheline.3)Itsimplifiesmemorym

您如何切成python列表?您如何切成python列表?May 02, 2025 am 12:14 AM

SlicingaPythonlistisdoneusingthesyntaxlist[start:stop:step].Here'showitworks:1)Startistheindexofthefirstelementtoinclude.2)Stopistheindexofthefirstelementtoexclude.3)Stepistheincrementbetweenelements.It'susefulforextractingportionsoflistsandcanuseneg

在Numpy陣列上可以執行哪些常見操作?在Numpy陣列上可以執行哪些常見操作?May 02, 2025 am 12:09 AM

numpyallowsforvariousoperationsonArrays:1)basicarithmeticlikeaddition,減法,乘法和division; 2)evationAperationssuchasmatrixmultiplication; 3)element-wiseOperations wiseOperationswithOutexpliitloops; 4)

Python的數據分析中如何使用陣列?Python的數據分析中如何使用陣列?May 02, 2025 am 12:09 AM

Arresinpython,尤其是Throughnumpyandpandas,weessentialFordataAnalysis,offeringSpeedAndeffied.1)NumpyArseNable efflaysenable efficefliceHandlingAtaSetSetSetSetSetSetSetSetSetSetSetsetSetSetSetSetsopplexoperationslikemovingaverages.2)

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

mPDF

mPDF

mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具