Monday, 27 October 2014

Bing Search Using Python

from bs4 import BeautifulSoup
import urllib2
class Bing:
def __init__(self):
self.__bing_url = "http://www.bing.com/search?q=%s&first=%s"
def search(self, _s_search):
i_page = 1
urls = []
while True:
try:
i_len_urls = len(urls)
response = urllib2.urlopen(self.__bing_url % (_s_search, i_page))
parsed_response = BeautifulSoup(response)
for h in parsed_response.findAll("h2"):
if h.a != None:
s_url = h.a['href']
if s_url in urls:
continue
elif s_url.startswith("http://") or s_url.startswith("https://") or s_url.startswith("ftp://"):
urls.append(s_url)
if len(urls) <= i_len_urls:
break
i_page = i_page + 10
except:
pass
return urls
b = Bing()
urls = b.search("bing")
print len(urls)
print '\n'.join(urls)
view raw bing.py hosted with ❤ by GitHub


Partial output of the above Python script is shown below:

194
http://www.bing.com/
http://en.wikipedia.org/wiki/Bing
http://www.facebook.com/Bing
https://twitter.com/Bing
http://www.bingtoolbar.com/en-US
http://www.youtube.com/user/bing
http://dictionary.reference.com/browse/bing
https://www.bingplaces.com/
http://www.thefreedictionary.com/Bing
http://43007555.r.msn.com/?ld=Dv_gnZ2ILAtTZR03P_3NWe4zVUCUyjVAbTHjJXdVp5pSJIfJyHoemQp9Uv_Wg6SZdsdEiXBfrZKs_SwYbqptHU6UyPdLC3XaylUED9ff_6c2EI5qNivG3i7FrVBH9_t3zJWhM63Utu8WncH9sAtJdZqlrMdY0&u=WindowFixed.com%2fone.php%3fremove%3dBing+Toolbar
http://3228102.r.msn.com/?ld=DvyB-NuRzghUJI5dayAAZZAzVUCUwcmn0K1FdQtdfmLCMQPUz3gc2BgUvJCHT8SlQgGQSrDGzPO89LJdfVC7Mg0Lnx22SO5JX7z0QT-CucKVea58oXWY5s-qrEpTLwMJKuUHNDA_udfLkpaKdohpE6EwelEPY&u=http%3a%2f%2fwww.ask.com%2fslp%3f%26q%3dwhat%2bis%2bbing%26sid%3d0c053e1c-66a9-4f01-ae7d-401fd0f4370f-0-us_msb%26kwid%3dbing%26cid%3d5787895127
http://advertise.bingads.microsoft.com/en-us/home
http://blogs.bing.com/webmaster/?p=8413
http://blogs.bing.com/
http://hk.bing.com/
http://advertise.bingads.microsoft.com/en-us/sign-up
http://en.wikipedia.org/wiki/Bing_(company)
http://www.microsoft.com/privacystatement/en-gb/bing/default.aspx
http://msdn.microsoft.com/en-us/library/dd877956.aspx
http://43007555.r.msn.com/?ld=d3odkhGgduicmifMUqm19BRTVUCUxkRtc4JCtSzSlX8koKvNTxhK6ZCc0xg7F2lL1VzUHlg0d091QCJzID_AFXhoaYV_qheV-DwL010iIeyGhkidZmY4BbDgkbsV4S7Y02EUygVci2nzFRJXxoML4rBHKB5GM&u=WindowFixed.com%2fone.php%3fremove%3dBing+Toolbar
https://addons.mozilla.org/en-US/firefox/addon/bing/
http://www.microsoft.com/maps/
https://www.bingmapsportal.com/
http://www.bingiton.com/
https://itunes.apple.com/us/app/bing/id345323231
http://www.merriam-webster.com/dictionary/bing
http://bing.en.softonic.com/
view raw bing.txt hosted with ❤ by GitHub

Sunday, 5 October 2014

Calculation of Beta of Stocks Using Python Libraries (Stock Risk Analysis)

As an example, let us consider Coca Cola (NYSE:KO). Historical Coca Cola stock data can be downloaded from Google Finance:
Historical NYSE:KO Data

Suppose we consider NYSE:SPY as the market indicator/index in calculating beta. Historical stock data of NYSE:SPY can be downloaded from Google Finance:
Historical NYSE:SPY Data

The following python script finds the beta value for market indicator and symbol historical data file names passed as command line parameters:

import numpy as np
from sklearn import datasets, linear_model
import sys
fh = open(sys.argv[1], 'r')
lines = fh.readlines()
fh.close()
market_x = []
for i in range(len(lines)-1):
if i==0:
continue
line_i = lines[i].strip().split(',')[4]
line_i_1 = lines[i+1].strip().split(',')[4]
rate = (float(line_i) - float(line_i_1))/(float(line_i_1))
market_x.append([rate])
fh = open(sys.argv[2], 'r')
lines = fh.readlines()
fh.close()
stock_y = []
for i in range(len(lines)-1):
if i==0:
continue
line_i = lines[i].strip().split(',')[4]
line_i_1 = lines[i+1].strip().split(',')[4]
rate = (float(line_i) - float(line_i_1))/(float(line_i_1))
stock_y.append([rate])
regr = linear_model.LinearRegression()
regr.fit(market_x, stock_y)
print 'Beta: %s' % regr.coef_[0][0]
view raw beta_cal.py hosted with ❤ by GitHub