2010-12-21 12 views
0
import re 
import string 
import shutil 
import os 
import os.path 
import time 
import datetime 
import math 
import urllib 
from array import array 
import random 

filehandle = urllib.urlopen('http://www.google.com/') #open webpage 
s = filehandle.read() #read 
print s #display 

#what i plan to do with it once i get the first part working 
#results = re.findall('[<td style="font-weight:bold;" nowrap>$][0-9][0-9][0-9][.][0-9][0-9][</td></tr></tfoot></table>]',s) 
#earnings = '$ ' 
#for money in results: 
#earnings = earnings + money[1]+money[2]+money[3]+'.'+money[5]+money[6] 
#print earnings 
#raw_input() 

これはこれまでのコードです。今私は、parse_Money.py、スクリプトの名前などのソリューションを与えるすべての他のフォーラムを見て、私はurllib.request.urlopenでそれをやってみたと私はそれをPython 2.5,2.6で実行しようとしました、 2.7。もし誰かが何か提案があれば、本当に歓迎すべきです、ありがとうございます!! --Matturllibに関しては、AttributeError: 'module'オブジェクトには属性 'urlopen'がありません。

--- EDIT --- 鋭い目を持つ誰もがそれを指摘することができますので、もし私が、私はまた、このコードを試してみました、それが働いていたので、イムは、構文エラーのそのいくつかの種類を考えます非常に感謝します。あなたが期待しているようurlopen方法がurllib.requestモジュールではなくurllibモジュールで利用可能であることを

import shutil 
import os 
import os.path 
import time 
import datetime 
import math 
import urllib 
from array import array 
import random 
b = 3 

#find URL 
URL = raw_input('Type the URL you would like to read from[Example: http://www.google.com/] :') 



while b == 3: 
    #get file name 
    file1 = raw_input('Enter a file name for the downloaded code:') 
    filepath = file1 + '.txt' 
    if os.path.isfile(filepath): 
     print 'File already exists' 
     b = 3 
    else: 
     print 'Filename accepted' 
     b = 4 

file_path = filepath 
#open file 
FileWrite = open(file_path, 'a') 


#acces URL 
filehandle = urllib.urlopen(URL) 

#display souce code 
for lines in filehandle.readlines(): 
    FileWrite.write(lines) 
    print lines 
print 'The above has been saved in both a text and html file' 

#close files 
filehandle.close() 
FileWrite.close() 
+1

が見えるあなたのpythonの正しいバージョンに対して、このスクリプトを実行します。オペレーティングシステムとは何ですか?ファイル名に 'urllib .__ file__'というファイルが入っていますか? – AndiDog

+0

IDLEで誤ったコードを実行しようとしましたが、うまく実行されました。私は 'urllib'以外のものは輸入しなかった。おそらくあなたはそれをやりたいと思うかもしれません。それ以外は、最初の例であなたのコードに間違っているとは思わない。 – anirvan

答えて

1

it appears

ルール - AttributeErrorを取得している場合、そのフィールド/操作は特定のモジュールに存在しません。

EDIT - これはPy3.xに有効な解決策であり、Py2.xには当てはまりません!

+0

私はurllib.requestをインポートするかどうか、または私がurllib.request.urlopenを呼び出す場合は同じことを言います。私が本当に困惑しているのは、あなたが上記のコマンドで今日早く走っていたことです。 – Matt

+1

彼は彼が3.xではなくPython 2.xを使っていると言いました!彼らはPython 3.0以降で 'urlopen'を' urllib.request'に移動しました。 – AndiDog

+0

私の悪い! AndiDogの観察を示す応答を編集しました。また、ファイルの場所を確認するという提案に加えて、 'urllib .__ dict __。get(" urlopen ")'を呼び出して、モジュールに実際にその操作が含まれているかどうかをチェックすることもできます。 – anirvan

0

urlopen関数は実際にはurllib2モジュールにあります。 import urllib2を試して、urllib2.urlopenを使用してください。

+2

それは両方にあります。 http://docs.python.org/library/urllib.html#urllib.urlopen – AndiDog

0

あなたはPython2を使用しているか、少なくともPython2を使用する予定です。 urlopenヘルパー機能は、Python2のurllibとurllib2の両方で利用できます。

は、あなたがこれを行うために必要なもの、あなたは*全く* Pythonインストールを台無しよう

C:\Python26\python.exe yourscript.py 
関連する問題