2011-10-25 7 views
0

私はPythonで新しく、いくつか質問があります!!ウェブページ内の文字列をファイルに保存せずに探しますか?

def extractdownloadurl(url): 

    uresponse = urllib2.urlopen(url) #open url 
    contents = uresponse.readlines() #readlines from url file 
    fo = open("test.html","w") #open test.html 
    for line in contents: 
     fo.write(line)#write lines from url file to text file 
    fo.close()#close text file 

    cadena = os.system('more test.html | grep uploads | grep zip >> cadena.html') 

    f = open("cadena.html","r") 
    text = f.read() 
    f.close() 


    match = re.search(r'href=[\'"]?([^\'" >]+)', text) 
    if match: 
     cadena=match.group(0) 


    texto = cadena[6:] 


    os.system('rm test.html') 
    os.system('rm cadena.html') 
    return texto 

これは私のウェブページをダウンロードし、いくつかの条件の後に1つのURLを取る私の機能です。できます。しかし、Webにファイルを保存するよりも効率的な方法を適用したいと考えています。私は、grepと似たようなものを、保存せずにファイルを読み込まないで作成したい(それは本当に遅いです)。そして、文字列にURLをコピーする他のより速い方法。

コンテンツをファイルに保存せずにコンテンツ内にURLを探すコードを記述してください。

私は多くの質問があることを知っていますが、あなたがそのすべてに答えると非常に感謝します。

答えて

1

これはあなたを助けるはずです。このスクリプトは、あなたの正規表現を使用してウェブページからのすべてのリンクを表示します:

import re, urllib 
page = urllib.urlopen("http://sebsauvage.net/index.html").read() 
urls = re.findall('href=[\'"]?([^\'" >]+)',page) 
for url in urls: 
    print url 
関連する問題