2012-02-10 9 views
6

2.5年前に同じ質問がDownloading a web page and all of its resource files in Pythonに届きましたが、回答につながることはなく、「関連トピックを参照してください」は同じことを本当に求めていません。WebサイトとリソースをダウンロードするためのPythonのwgetと同等

ファイルからすべてを見ることができるように、ページ上のすべてをダウンロードします。

コマンド

wgetの--page必要条件の--domains = DOMAIN --no-親--html-延長--convertリンク--restrict-ファイル名=窓

私が必要としているとおりです。しかし、私たちはポータブルでなければならない他のものと結びつけたいので、Pythonにする必要があります。

私はBeautiful Soup、scrapyを見てきましたが、その周辺にはさまざまなスパイダーが掲載されていましたが、これらはすべてデータやリンクを巧妙で特殊な方法で扱うようです。これらを使って私が望むことをするのは、簡単な方法が必要であると確信しているときに、すべてのリソースを見つけるために多くの作業が必要になるようです。

おかげで非常に多くの

+0

インポートurllibはのurllib.urlretrieve( 'http://www.somesite.com/fileをやりたいと思っ役立つはずです.whatever '、'ファイル名をダウンロードする 'など) – CR0SS0V3R

+1

そのような方法で単数のファイルをダウンロードできることはわかっていますが、クローラを使用して、必要なすべてのファイルを見つけるために多くの条件を設定する必要がありますウェブサイトの一部をオフラインで見ることができるすべて)。 Pythonでダウンロードしたウェブサイトと必要条件の周りに何かが存在する必要がありますか? – Conrad

+0

forループ内の解析関数を使用して、ダウンロードしたファイル内のリンクを検索することができます(またはどこからでも読み取ることができます) – CR0SS0V3R

答えて

3

あなたは手元に仕事のための適切なツールを使用する必要があります。

サイトをスパイダーしてページをディスクに保存したい場合、Pythonはおそらくそのための最良の選択肢ではありません。オープンソースプロジェクトは、誰かがその機能を必要とするときに機能を取得し、wgetがその仕事をうまくやっているので、誰もそれを置き換えるためにPythonライブラリを書くのを心配していません。

wgetはPythonインタプリタを持つプラットフォーム上で実行されることを考慮すると、wgetを使用できない理由はありますか?

+0

あなたは、誰もがPythonのために書くつもりはないという良い点を作っています。私がwgetのルートを追求していない唯一の理由は、私がPythonでそれをするように求められたことです...私は依存関係を減らしたいと思っています。 私たちは狭い使い方のためにPythonでこのツールを書いています。許可の場合はここに投稿します。 – Conrad

1

私の同僚はこのコードを書きました。私が信じている他の情報源からたくさんの情報が得られました。我々のシステムのためのいくつかの特定の癖を持っているかもしれませんが、それは誰もが返信用と同じ

""" 
    Downloads all links from a specified location and saves to machine. 
    Downloaded links will only be of a lower level then links specified. 
    To use: python downloader.py link 
""" 
import sys,re,os,urllib2,urllib,urlparse 
tocrawl = set([sys.argv[1]]) 
# linkregex = re.compile('<a\s*href=[\'|"](.*?)[\'"].*?') 
linkregex = re.compile('href=[\'|"](.*?)[\'"].*?') 
linksrc = re.compile('src=[\'|"](.*?)[\'"].*?') 
def main(): 
    link_list = []##create a list of all found links so there are no duplicates 
    restrict = sys.argv[1]##used to restrict found links to only have lower level 
    link_list.append(restrict) 
    parent_folder = restrict.rfind('/', 0, len(restrict)-1) 
    ##a.com/b/c/d/ make /d/ as parent folder 
    while 1: 
     try: 
      crawling = tocrawl.pop() 
      #print crawling 
     except KeyError: 
      break 
     url = urlparse.urlparse(crawling)##splits url into sections 
     try: 
      response = urllib2.urlopen(crawling)##try to open the url 
     except: 
      continue 
     msg = response.read()##save source of url 
     links = linkregex.findall(msg)##search for all href in source 
     links = links + linksrc.findall(msg)##search for all src in source 
     for link in (links.pop(0) for _ in xrange(len(links))): 
      if link.startswith('/'): 
       ##if /xxx a.com/b/c/ -> a.com/b/c/xxx 
       link = 'http://' + url[1] + link 
      elif ~link.find('#'): 
       continue 
      elif link.startswith('../'): 
       if link.find('../../'):##only use links that are max 1 level above reference 
        ##if ../xxx.html a.com/b/c/d.html -> a.com/b/xxx.html 
        parent_pos = url[2].rfind('/') 
        parent_pos = url[2].rfind('/', 0, parent_pos-2) + 1 
        parent_url = url[2][:parent_pos] 
        new_link = link.find('/')+1 
        link = link[new_link:] 
        link = 'http://' + url[1] + parent_url + link 
       else: 
        continue 
      elif not link.startswith('http'): 
       if url[2].find('.html'): 
        ##if xxx.html a.com/b/c/d.html -> a.com/b/c/xxx.html 
        a = url[2].rfind('/')+1 
        parent = url[2][:a] 
        link = 'http://' + url[1] + parent + link 
       else: 
        ##if xxx.html a.com/b/c/ -> a.com/b/c/xxx.html 
        link = 'http://' + url[1] + url[2] + link 
      if link not in link_list: 
       link_list.append(link)##add link to list of already found links 
       if (~link.find(restrict)): 
       ##only grab links which are below input site 
        print link ##print downloaded link 
        tocrawl.add(link)##add link to pending view links 
        file_name = link[parent_folder+1:]##folder structure for files to be saved 
        filename = file_name.rfind('/') 
        folder = file_name[:filename]##creates folder names 
        folder = os.path.abspath(folder)##creates folder path 
        if not os.path.exists(folder): 
         os.makedirs(folder)##make folder if it does not exist 
        try: 
         urllib.urlretrieve(link, file_name)##download the link 
        except: 
         print "could not download %s"%link 
       else: 
        continue 
if __name__ == "__main__": 
    main() 

おかげ

+0

私はプログラミングの初心者です。どうすればこのコードを使用することができますか?また、Webページにリンクされているものをすべてダウンロードし、ローカルで開くことを希望しています。また、Pythonでそれを行うように求められています。 –

+0

どこにリンクを張り、どこにページが保存されますか? –

+1

ouch .. htmlパーサを使用する –

関連する問題