2 simple ways to get stock data

Source: Internet
Author: User

http://blog.sciencenet.cn/home.php?mod=space&uid=461456&do=blog&id=455211
1. Original address: http://www.21andy.com/blog/20090530/1313.html

Real-time Stock data Interface Daquan

There are two ways to get the stock data now:
1. Http/javascript Interface Fetch data
2. Web-service interface

1.http/javascript interface Fetch data

1.1Sina Stock Data Interface

Take the Great Qin Railway (stock code: 601006) As an example, if you want to get its latest market, simply visit the new stock data interface of the wave:

http://hq.sinajs.cn/list=sh601006

This URL returns a string of text, for example:

var hq_str_sh601006= "Great Qin Railway, 27.55, 27.25, 26.91, 27.55, 26.20, 26.91, 26.92,
22114263, 589824680, 4695, 26.91, 57590, 26.90, 14700, 26.89, 14300,
26.88, 15100, 26.87, 3100, 26.92, 8900, 26.93, 14230, 26.94, 25150, 26.95, 15220, 26.96, 2008-01-11, 15:05:32 ";

The string is stitched together by a lot of data, with different meanings separated by commas, and the sequence number starts with 0, according to the programmer's idea.

0: "Great Qin Railway", Stock name;
1: "27.55″, open today;
2: "27.25″, yesterday close;
3: "26.91″, current price;
4: "27.55″, today's highest price;
5: "26.20″, today's lowest price;
6: "26.91″, bid price, namely" buy one "offer;
7: "26.92″, bid price, namely" sell one "offer;
8: "22114263″, the number of shares traded, as the stock trading with 100 shares as the basic unit, so when used, the value is usually divided by 100;
9: "589824680″, deal amount, unit for" Yuan ", for a glance, usually" million "for the transaction amount of units, so usually the value divided by 10,000;
10: "4695″," "Buy a" application 4695 shares, that is, 47 lot;
11: "26.91″", "buy one" offer;
12: "57590″," buy two "
13: "26.90″," buy two "
14: "14700″," buy three "
15: "26.89″," buy three "
16: "14300″," buy four "
17: "26.88″," buy four "
18: "15100″", "Buy Five"
19: "26.87″", "Buy Five"
20: "3100″," sell one "declare 3100 shares, namely 31 lot;
21: "26.92″", "sell one" offer
(22, 23), (24, 25), (26,27), (28, 29) respectively "sell two" to "sell four"
30: "2008-01-11″, date;
31: "15:05:32″, time;

An example of a simple JavaScript application:

<Script type="Text/javascript" src="http://hq.sinajs.cn/list=sh601006" CharSet="gb2312"></script>
<script type= "Text/JavaScript">
var elements=hq_str_sh601006.split (",");
document.write (" Current  Price:"+elements[3]);
</script>

This code outputs the current stock price of the Great Qin Railway (stock code: 601006)

Current price:14.20

If you want to query multiple shares at the same time, then add a comma at the end of the URL, plus the stock code on it, for example, you want to query the Da Qin Railway (601006) and Datong Coal (601001) quotes, so use the URL:

http://hq.sinajs.cn/list=sh601003,sh601001

Query the market index, for example, the Shanghai Composite Index (000001):

http://hq.sinajs.cn/list=s_sh000001

The data returned by the server is:

var hq_str_s_sh000001= "Shanghai index, 3094.668,-128.073,-3.97,436653,5458126";

Data meanings are: Index name, current point, current price, rate of rise and fall, turnover (hand), turnover (million);

Query Szse Component number:

http://hq.sinajs.cn/list=s_sz399001

For stock candlestick Chart, the acquisition of daily charts and so on can be obtained by requesting http://image.sinajs.cn/..../.../*.gif this URL, where * represents the stock code, as described below:

View Daily Candlestick Chart:

Http://image.sinajs.cn/newchart/daily/n/sh601006.gif

Time-sharing line query:

Http://image.sinajs.cn/newchart/min/n/sh000001.gif

Daily Candlestick Query:

Http://image.sinajs.cn/newchart/daily/n/sh000001.gif

Weekly Candlestick Query:

Http://image.sinajs.cn/newchart/weekly/n/sh000001.gif

Monthly candlestick Query:

Http://image.sinajs.cn/newchart/monthly/n/sh000001.gif

1.2 Baidu&google's financial data
In Baidu, when searching for a stock ticker in Google, information about the stock is displayed in the headlines, such as when Google searches for 601006,
The first search results are as follows:
By clicking on the image on the left we find that the image is linked to the Sina Financial channel, which means that Google stock data acquisition is also obtained from Sina. After the capture packet analysis, found that Google is also used in 1.1 of the interface described.

Baidu's stock data from Baidu's financial channel
http://stock.baidu.com/


==================================
2. The author of this method on the page said no reprint, so excerpt a paragraph:The rest is in (actually the Java version of the method above) http://www.iteye.com/topic/169821

Stocks for some time, found that the current stock market software in many cases is not too easy to use, I like to see stocks in the real-time trading records, if at the same time pay more attention to the stock, I used the market software did not find the same time to watch a number of stock markets trading data functions, In addition, the use of current market software is not very convenient at work. So I intend to write a special market software, but the data source is the most difficult problem, after searching and unremitting efforts, found two ways:

2.1 Call via WebService http://www.webxml.com.cn/zh_cn/web_services.aspx , the site offers free and chargeable services, but it is not practical to limit the number of requests per day for free.

In summary: Call Sina dedicated JS server to parse the data, this way I used for a long time, the speed is quite good, and sometimes than the special market software real-time data is also faster, the following is the part of the code to obtain data:

3. Ways to obtain historical data from waves:

http://blog.163.com/fluxray_sensor/blog/static/2965101520085213574929/

These days on the Internet to find the stock of historical data to study, and finally found a good interface, from Yahoo, is found on a blog, the address is: http://www.bizeway.net/read.php?317. But this is not the first I found the interface, the most initial I would like to directly parse the wave of new historical trading page, but the data is not very full, only about 2 months of data. The following I pasted the parsing script, mainly to demonstrate the infinite charm of regular expressions, because the key to the resolution is undoubtedly in a regular expression!

$stockCode = 600000
$url = "http://money.finance.sina.com.cn/corp/go.php/vms_markethistory/stockid/{0}.phtml"-F $stockCode
$WC = New-object System.Net.WebClient
$content = $WC. Downloadstring ($url)

$reg = "<a target= ' _blank ' \s+href= ' http://biz.finance.sina.com.cn/stock/history_min.php\?symbol=sh\d{6}& Date=\d{4}-\d{2}-\d{2} ' >\s* ([^\s]+) \s+\s*

\s*<td[^\d]* ([^<]*) \s+<td[^\d]* ([^<]*) \s+<td[^\d]* ([^<]*] \s+<td[^\d]* ([^<]*) \s+ "
$result = [Regex]::matches ($content, $reg)

foreach ($item in $result)
{
$date = $item. GROUPS[1]. Value # time
$opening = $item. GROUPS[2]. Value # Open
$maxHigh = $item. GROUPS[3]. Value # Highest
$closing = $item. GROUPS[4]. Value # Close
$maxLow = $item. GROUPS[5]. Value # Lowest
Write-host $date $opening $maxHigh $closing $maxLow
}


4. Programming to get historical data for all stocks

Http://hi.baidu.com/pinotwu/blog/item/0adb7b7bfc0f53e12f73b3e8.html

2010-03-24 02:22
The previous article was written on the portfolio calculation, where the large amount of data used was not easy to find. A variety of free stock software basically can only export a stock of the historical price, I do not want to sit in front of the computer silly mouse point more than 10 20 hours.

Below to get the method in detail to write, hope to be useful to everyone, is also a record for themselves.

After looking at the big financial websites, we found that Yahoo could offer some good free lunches. In Yahoo Finance (http://yahoo.compass.cn/stock/) download Historical stock information ("Quotes"-"Historical price"), you can see the "XML Data downloaded: Day-week" this. Click on the day-week, the score page of the HTML data good parsing. As for the online popular Sina stock Data interface, do not provide historical prices, only the current price (or have I do not know). Click on the day link to discover that:
Http://yahoo.compass.cn/stock/xml/000001.ss_day.xml

Very good. It's clear at first sight. The data are:

<label ref= "Date" > Market date </label>
<label ref= "Open" > Open </label>
<label ref= "High" > Top price </label>
<label ref= "Low" > Lowest price </label>
<label ref= "Close" > Close price </label>
<label ref= "Volume" > Volume </label>
<label ref= "Amount" > Turnover </label>

This has developed!

With Python, it's easy to write multi-threaded crawl web programs.
Thread Libraries: Threading
Web crawler: Urllib2
XML parsing: Xml.dom
HTML parsing: Sgmlpaser

Note that due to the large number of data, so that the failure of the crawl is a lot of factors, so do error handling. Can't catch it once, I grabbed it 2 times! I was so shameless to deal with: found that the crawl failed to cycle him 5 times! Who gave you a free lunch? I am obliged to fulfill the obligations of the arbitrage. Money to buy data? More money, huh? (If you find out which free quote software can download all the historical data for all stocks, don't tell me).

And in Yahoo Finance, found that the total market capitalization is not good crawl, not in its source code appears. Using AJAX? Nor did they find the relevant JavaScript code. Well, we're not familiar with web technology.

Later found in the Baidu financial information provided better crawl, use it.

As for the company code, one thousand or two thousand, Manual check also very laborious. Fortunately, the Exchange homepage is easy to find. This aspect of the Szse than SSE do a bit better, directly have Excel file download.

After debugging, OK. It's all scratched down. The relevant Python code can be downloaded by clicking on the link below.
Http://ishare.iask.sina.com.cn/f/7166441.html

We enclose the listed company code files. Fetch_market_cap.py/fetch_data.py is the main program, the other is the module. See the Readme for details.

Crawled data are CSV files, with python/perl/awk/sed processing is extremely convenient.
Buy data? Joke! will be free to the end.

Source: http://www.lxway.com/946486042.htm

2 simple ways to get stock data

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.