1

I have the below code which has the list r3000, a list of links to save as html.

Is it possible to save the files with different names using a separate list?

For example, r3000 would include the link ('http://research.investors.com/quotes/nyse-agilent-technologies-inc-a.htm?fromsearch=1') then have another list called r3000sym that is ['a','','',...]. Then file would be saved as a.html.

import time
import urllib2
from urllib2 import urlopen

r3000 = ['http://research.investors.com/quotes/nyse-agilent-technologies-inc-a.htm?fromsearch=1',
         'http://research.investors.com/quotes/nyse-alcoa-inc-aa.htm?fromsearch=1',
         'http://research.investors.com/quotes/nasdaq-american-airlines-group-aal.htm?fromsearch=1',
         'http://research.investors.com/quotes/amex-altisource-asset-mgmt-aamc.htm?fromsearch=1',
         'http://research.investors.com/quotes/nyse-aarons-inc-aan.htm?fromsearch=1',
         'http://research.investors.com/quotes/nasdaq-applied-optoelectronics-aaoi.htm?fromsearch=1',
         'http://research.investors.com/quotes/nasdaq-a-a-o-n-inc-aaon.htm?fromsearch=1',
         'http://research.investors.com/quotes/nyse-advance-auto-parts-inc-aap.htm?fromsearch=1']

def yahooKeyStats(stock):
    try:
        site= stock
        hdr = {'User-Agent': 'Mozilla/5.0'}
        req = urllib2.Request(site,headers=hdr)
        page = urllib2.urlopen(req).read()


        dataFile = 'a.html'
        f = open(dataFile,'a')
        f.write(page)
        f.close()

        print 'Done',stock

    except Exception,e:
        print str(e)


for eachStock in r3000:
    yahooKeyStats(eachStock)
2
  • 1
    Sounds like you're looking for zip(). Commented Jun 10, 2015 at 18:42
  • You can loop over two lists using zip() Commented Jun 10, 2015 at 18:42

3 Answers 3

2

itertools is what you are looking for:

import itertools
for it1,it2 in itertools.izip(list1,list2):
    print(it1,it2)

Zip takes 2 lists and makes tupled list out of them, itertools allows you to iterate at the same time.

Sign up to request clarification or add additional context in comments.

Comments

1

Use a two-tuple to store url and name in one list:

import time
import urllib2
from urllib2 import urlopen

r3000 = [
    ('a.html', 'http://research.investors.com/quotes/nyse-agilent-technologies-inc-a.htm?fromsearch=1'),
    ('b.html', 'http://research.investors.com/quotes/nyse-alcoa-inc-aa.htm?fromsearch=1'),
]

def yahooKeyStats(name, stock):
    try:
        site= stock
        hdr = {'User-Agent': 'Mozilla/5.0'}
        req = urllib2.Request(site,headers=hdr)
        page = urllib2.urlopen(req).read()

        with open(name,'a') as f:
            f.write(page)

        print 'Done',stock

    except Exception,e:
        print str(e)


for name, stock in r3000:
    yahooKeyStats(name, stock)

Comments

1

You're probably looking for the zip() function. It creates an iterable of tuples based on the two lists given, which you can assign as a local loops variable. see: this post

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.