how to run in xp?

守株待兔 1248283536 at qq.com
Wed Sep 28 13:50:04 CEST 2011


it can run ,but there is still a problem ,nothing in my file.
please run the code in xp+python32
import urllib.request, urllib.parse, urllib.error
exchange=['NASDAQ','NYSE','AMEX']
for down in exchange:
    url='http://www.nasdaq.com/screening/companies-by-industry.aspx?exchange='+down+'&render=download'
    file=urllib.request.urlopen(url).read()
    print (file)
what you get is:

>>> 
b''
b''
b''
>>> 

how to fix it?
------------------ Original ------------------
From:  "Peter Otten"<__peter__ at web.de>;
Date:  Wed, Sep 28, 2011 04:04 PM
To:  "python-list"<python-list at python.org>; 

Subject:  Re: how to run in xp?

 
=?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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110928/6d692d2c/attachment.html>


More information about the Python-list mailing list