Demo entry 6354926

123456

   

Submitted by lkjhgfd on Apr 11, 2017 at 16:15
Language: Python. Code size: 1.8 kB.

#!/usr/bin/python
#-*-coding:utf-8 -*-

import urllib2,csv,cookielib
import time
import random
#site = "http://xueqiu.com/S/AAPL/historical.csv"
#site= "http://www.nseindia.com/live_market/dynaContent/live_watch/get_quote/getHistoricalData.jsp?symbol=JPASSOCIAT&fromDate=1-JAN-2012&toDate=1-AUG-2012&datePeriod=unselected&hiddDwnld=true"
hdr = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11',
       'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
       'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
       'Accept-Encoding': 'none',
       'Accept-Language': 'en-US,en;q=0.8',
       'Connection': 'keep-alive'}

#req = urllib2.Request(site, headers=hdr)
symbolTest = 'APPL'
Exchange = 'NASDAQ'

with open(Exchange +'.csv') as csvfile:
        reader = csv.DictReader(csvfile)
        for row in reader:
            print(row['Symbol'], row['Name'])
            symbol = row['Symbol'].strip()
            if '^' not in symbol:
              try:
                    #href="http://chart.finance.yahoo.com/table.csv?s=ABIL&a=3&b=1&c=2014&d=3&e=1&f=2017&g=d&ignore=.csv"
                    site="http://chart.finance.yahoo.com/table.csv?s="+ symbol+"&a=3&b=1&c=2000&d=3&e=1&f=2017&g=d&ignore=.csv"
                    req = urllib2.Request(site, headers=hdr)
                    page = urllib2.urlopen(req)
                    with open(Exchange + '/'+symbol+'.csv','w') as symbolCSV:
                         symbolCSV.write(page.read())  
              except urllib2.HTTPError, e:
                    print e.fp.read()             
            else:
              print 'symbol contains ^, not valid, passed...'

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).