=?gbk?B?ytjW6rT9zcM=?= wrote: > #coding:utf-8 > import urllib > exchange=['NASDAQ','NYSE','AMEX'] > for down in exchange: > myfile=open('./'+down,'w') > url='http://www.nasdaq.com/screening/companies- \ > by-industry.aspx?exchange='+down+'&render=download' > file=urllib.urlopen(url).read() myfile.write(file) > print ('ok',down) > myfile.close() > > it can run in ubuntu+python2.6 ,when it run in window xp+python32,the > output is Traceback (most recent call last): > File "C:\Python32\getcode.py", line 8, in <module> > file=urllib.urlopen(url).read() > AttributeError: 'module' object has no attribute 'urlopen' > > i change it into: > #coding:utf-8 > import urllib.request > exchange=['NASDAQ','NYSE','AMEX'] > for down in exchange: > myfile=open('./'+down,'w') > url='http://www.nasdaq.com/screening/companies-by- industry.aspx?exchange='+down+'&render=download' > file=urllib.request.urlopen(url).read() > myfile.write(file) > print ('ok',down) > myfile.close() > > the output is : > Traceback (most recent call last): > File "C:\Python32\getcode.py", line 9, in <module> > myfile.write(file) > TypeError: must be str, not bytes > > how to make it run in xp+python32?
The problem here is the switch from Python 2 to 3. Python 3 has some backwards-incompatible Syntax changes along with changes to classes and library organisation. The good news is that there's a tool, 2to3, that can handle most of these changes: $ cat getcode.py #coding:utf-8 import urllib exchange=['NASDAQ','NYSE','AMEX'] for down in exchange: myfile=open('./'+down,'wb') url='http://www.nasdaq.com/screening/companies-by- industry.aspx?exchange='+down+'&render=download' file=urllib.urlopen(url).read() myfile.write(file) print 'ok', down myfile.close() $ cp getcode.py getcode3.py $ 2to3-3.2 getcode3.py -w RefactoringTool: Skipping implicit fixer: buffer RefactoringTool: Skipping implicit fixer: idioms RefactoringTool: Skipping implicit fixer: set_literal RefactoringTool: Skipping implicit fixer: ws_comma RefactoringTool: Refactored getcode3.py --- getcode3.py (original) +++ getcode3.py (refactored) @@ -1,10 +1,10 @@ #coding:utf-8 -import urllib +import urllib.request, urllib.parse, urllib.error exchange=['NASDAQ','NYSE','AMEX'] for down in exchange: myfile=open('./'+down,'wb') url='http://www.nasdaq.com/screening/companies-by- industry.aspx?exchange='+down+'&render=download' - file=urllib.urlopen(url).read() + file=urllib.request.urlopen(url).read() myfile.write(file) - print 'ok', down + print('ok', down) myfile.close() RefactoringTool: Files that were modified: RefactoringTool: getcode3.py $ cat getcode3.py #coding:utf-8 import urllib.request, urllib.parse, urllib.error exchange=['NASDAQ','NYSE','AMEX'] for down in exchange: myfile=open('./'+down,'wb') url='http://www.nasdaq.com/screening/companies-by- industry.aspx?exchange='+down+'&render=download' file=urllib.request.urlopen(url).read() myfile.write(file) print('ok', down) myfile.close() $ python3.2 getcode3.py ok NASDAQ ok NYSE ok AMEX -- http://mail.python.org/mailman/listinfo/python-list