我有以下代码,其中包含r3000
列表,一个保存为html的链接列表。
是否可以使用单独的列表保存具有不同名称的文件?
例如,r3000
会包含该链接(“http://research.investors.com/quotes/nyse-agilent-technologies-inc-a.htm?fromsearch=1”),然后会有另一个名为r3000sym
的列表['a','','',...]
。然后文件将保存为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)
答案 0 :(得分:2)
itertools
:
import itertools
for it1,it2 in itertools.izip(list1,list2):
print(it1,it2)
Zip获取2个列表并从中生成tupled列表,itertools
允许您同时进行迭代。
答案 1 :(得分:1)
使用两元组将url和name存储在一个列表中:
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)
答案 2 :(得分:1)
您可能正在寻找zip()函数。它根据给定的两个列表创建一个可迭代的元组,您可以将其指定为本地循环变量。见:nose documentation