URLアドレスが一意ではないウェブサイトからのZIPファイルのダウンロードを自動化する必要があります。データは右の関連ダウンロードの下にリンクされています。私はPythonやスクリプトの経験がないので、初心者が使用できるツールが必要になります。オートメーションにファイルの解凍が含まれているかどうかもわかります。ウェブサイトからZipファイルをダウンロードするにはどうすればいいですか?
私は助力/助言をいただきありがとうございます。
URLアドレスが一意ではないウェブサイトからのZIPファイルのダウンロードを自動化する必要があります。データは右の関連ダウンロードの下にリンクされています。私はPythonやスクリプトの経験がないので、初心者が使用できるツールが必要になります。オートメーションにファイルの解凍が含まれているかどうかもわかります。ウェブサイトからZipファイルをダウンロードするにはどうすればいいですか?
私は助力/助言をいただきありがとうございます。
あなたの出発地としてBeautifulSoupとrequestsをご覧ください。私は1日1回実行され、新しいもののzipファイルリンクをチェックするスクリプトを書くでしょう。
import requests
from bs4 import BeautifulSoup
url = 'http://phmsa.dot.gov/pipeline/library/data-stats/distribution-transmission-and-gathering-lng-and-liquid-annual-data'
r = requests.get(url)
soup = BeautifulSoup(r.text, 'html.parser')
all_hrefs = soup.find_all('a')
all_links = [link.get('href') for link in all_hrefs]
zip_files = [dl for dl in all_links if dl and '.zip' in dl]
これは、あなたにそのメインのランディングページ上のすべてのzipファイルのリストを(拡張子が小文字で常にあると仮定した場合)を取得します。私はこの情報をSQLiteデータベースに保存するだけで、1つの行に各zipファイルがあるプレーンテキストファイルでも保存できます。スクリプトを実行すると、上のコードを使用してリンクを取得し、データベース(またはテキストファイル)を開いて比較し、新しいものがあるかどうかを比較します。
新しいリンクが見つかった場合は、素晴らしいrequestsライブラリを使用してダウンロードできます。追加解凍 -
import os
import requests
from bs4 import BeautifulSoup
url = 'http://phmsa.dot.gov/pipeline/library/data-stats/distribution-transmission-and-gathering-lng-and-liquid-annual-data'
root = 'http://phmsa.dot.gov/'
r = requests.get(url)
soup = BeautifulSoup(r.text, 'html.parser')
all_hrefs = soup.find_all('a')
all_links = [link.get('href') for link in all_hrefs]
zip_files = [dl for dl in all_links if dl and '.zip' in dl]
download_folder = '/home/mdriscoll/Downloads/zip_files'
if not os.path.exists(download_folder):
os.makedirs(download_folder)
for zip_file in zip_files:
full_url = root + zip_file
r = requests.get(full_url)
zip_filename = os.path.basename(zip_file)
dl_path = os.path.join(download_folder, zip_filename)
with open(dl_path, 'wb') as z_file:
z_file.write(r.content)
更新#2:あなたはそれを実行
import os
import requests
root = 'http://phmsa.dot.gov/'
download_folder = '/path/to/download/zip/files/'
for zip_file in zip_files:
full_url = root + zip_file
r = requests.get(full_url)
zip_filename = os.path.basename(zip_file)
dl_path = os.path.join(download_folder, zip_filename)
with open(dl_path, 'wb') as z_file:
z_file.write(r.content)
はここだけでページ上のすべての時間を、すべてのzipファイルをダウンロードします完全な例です:あなたはこのようなものが必要でしょう機能:
import os
import requests
import zipfile
from bs4 import BeautifulSoup
url = 'http://phmsa.dot.gov/pipeline/library/data-stats/distribution-transmission-and-gathering-lng-and-liquid-annual-data'
root = 'http://phmsa.dot.gov/'
r = requests.get(url)
soup = BeautifulSoup(r.text, 'html.parser')
all_hrefs = soup.find_all('a')
all_links = [link.get('href') for link in all_hrefs]
zip_files = [dl for dl in all_links if dl and '.zip' in dl]
download_folder = '/home/mdriscoll/Downloads/zip_files'
if not os.path.exists(download_folder):
os.makedirs(download_folder)
tries = 0
for zip_file in zip_files:
full_url = root + zip_file
zip_filename = os.path.basename(zip_file)
dl_path = os.path.join(download_folder, zip_filename)
if os.path.exists(dl_path):
# you have already downloaded this file, so skip it
continue
while tries < 3:
r = requests.get(full_url)
dl_path = os.path.join(download_folder, zip_filename)
with open(dl_path, 'wb') as z_file:
z_file.write(r.content)
# unzip the file
extract_dir = os.path.splitext(os.path.basename(zip_file))[0]
try:
z = zipfile.ZipFile(dl_path)
z.extractall(os.path.join(download_folder, extract_dir))
break
except zipfile.BadZipfile:
# the file didn't download correctly, so try again
# this is also a good place to log the error
pass
tries += 1
私は時折、ファイルが正常にダウンロードすることはないと私はBadZipFile
になるだろう私のテストに気づきましたエラーが発生しましたので、ダウンロードする次のファイルに進む前に3回試してみるコードを追加しました。
OK、私は自分のコンピュータにPythonをダウンロードしてインストールすることができました。また、 "要求"と "BeautifulSoup"もインストールできました。私はPythonの初心者ですから、コードを実行するためにPyCharm Eduをインストールしました。 PhyCharmEduを使って両方のスクリプトを実行しようとしましたが、何も戻ってこなかった。 zipファイルのURLを見つけることができました(下記参照)。ファイルを自動的にダウンロードする方法を教えてください。また、スクリプトを使用してファイルを自動的に解凍することは可能ですか? http://www.phmsa.dot.gov/staticfiles/PHMSA/DownloadableFiles/Pipeline2data/annual_hazardous_liquid_2010_present.zip – Gloria
すべてのzipファイルをダウンロードする完全な例を追加しましたが、既にそれをダウンロードしました。それは簡単に追加できます。 Pythonには、それらを解凍するために動作する 'zipfile'モジュールがあります - https://docs.python.org/2/library/zipfile.html –
Mike、スクリプトをありがとう。それは完全に動作します。ファイルを解凍するためのドキュメントを見ていきます。 – Gloria
こんにちは、歓迎、StackOverflow。あなたは、あなたの答えを改善し、いくつかの努力を示し、詳細を提供する必要があります。 [How to ask](http://stackoverflow.com/help/how-to-ask)をお読みください。ソフトウェアの推奨を求める質問のタイプはここでは適切ではありません。しかし、あなたは[Flashget](http://www.flashget.com/)があなたの目的に役立つかどうか試してみることができます。 – iled