Skip to content

Instantly share code, notes, and snippets.

@AkiyonKS
Created August 9, 2022 02:05
Show Gist options
  • Save AkiyonKS/fb90a065e9d116eede6712f2f2a8baa7 to your computer and use it in GitHub Desktop.
Save AkiyonKS/fb90a065e9d116eede6712f2f2a8baa7 to your computer and use it in GitHub Desktop.
fetch raillab images
import request
import pandas as pd
# urlを指定して画像を取得、保存する関数
def fetch_image(url, save_file_name):
print(save_file_name, url)
purl = "https://raillab.jp"
  # 読み込む画像のurlを指定
url_r = purl + url
  # 保存する画像のファイル名を指定
save_file_name_r = "jpg/" + str(save_file_name) + ".jpg"
try:
response = requests.get(url_r, timeout=1000)
response.raise_for_status()
print(response.status_code)
image = response.content
with open(save_file_name_r, "wb") as file:
file.write(image)
except requests.exceptions.RequestException as e:
print("エラー : ",e)
# テキストデータ読み込み
df = pd.read_csv("../csv/trains.csv")
# photo_idとsrc(画像のurl)を抽出
df2 = df.loc[0:, ["photo_id", "src"]]
# 画像の取得と保存を繰り返し実行
df2.apply((lambda x: fetch_image(x.src, x.photo_id)), axis=1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment