Python script: Get Stock Information

Source: Internet
Author: User

Someone asked me about how to use python to obtain stock information. The data on sina finance is controlled by js, real-time information is obtained based on the stock code and displayed in a user-friendly manner.
First, Sina's url allows us to get stock information: http://hq.sinajs.cn /? List = sh600000
Then, I simply wrote a program to extract useful information such as the stock name, current price, and increase/decrease. Originally, it was not difficult. It was only in the aspect of digital processing and formatting and output that I found some materials to see.
The output is as follows:
---------------------
Stock name: Hypergraph software increase/decrease: 2.07 latest price: 22.73
Stock name: surging news:-2.91 latest price: 19.33
Stock name: Neusoft group rose or fell:-3.93 latest price: 10.52
Stock name: Fuling Electric Power Up or down: 0.0 latest price: 11.31
Stock name: Medium gold, up or down:-2.01 latest price: 22.41
----------------------
Simple code implementation is as follows:
#! /Usr/bin/python3
# Coding = UTF-8

"'
Created on Nov 9, 2011

@ Author: laugh all over the world
"'

Import urllib. request

Def get_price (code ):
Url = 'HTTP: // hq.sinajs.cn /? List = % s' % code
Req = urllib. request. Request (url)
# If you do not need to set a proxy, the following set_proxy does not need to be called. Because the company's network requires a proxy to connect to the Internet, there is set_proxy...
Req. set_proxy ('proxy .XXX.com: 911 ', 'http ')
Content = urllib. request. urlopen (req). read ()
Str = content. decode ('gbk ')
Data = str. split ('"') [1]. split (',')
Name = "%-6s" % data [0]
Price_current = "%-6s" % float (data [3])
Change_percent = (float (data [3])-float (data [2]) * 100/float (data [2])
Change_percent = "%-6s" % round (change_percent, 2)
Print ("stock name: {0} rise/fall: {1} latest price: {2}". format (name, change_percent, price_current ))

Def get_all_price (code_list ):
For code in code_list:
Get_price (code)

Code_list = ['sz300036', 'sz000977 ', 'sh600718', 'sh600452', 'sh600489']
Get_all_price (code_list)


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.