fe ner
fe ner

Reputation: 1769

Python use 2 lists in 1 loop

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)

Upvotes: 1

Views: 111

Answers (3)

csunday95
csunday95

Reputation: 1319

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

Upvotes: 1

Daniel
Daniel

Reputation: 42748

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)

Upvotes: 1

John
John

Reputation: 2480

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.

Upvotes: 2

Related Questions