自身が書いてるコードにて、Instagramの投稿写真の画像URLを取得しています。
また、画像の様に、それぞれの投稿URLのみだけがプリントされるように指定しました。
この取得した画像URLを全て指定した任意のフォルダに保存したいです。
サイトを参考にしていますが、
URL = '欲しい画像のURL' # URL入力
の文で、欲しい画像のURL単体を書き1枚画像を取得することは可能なのかなと思っているのですが、自身がやっているように、取得した大量の画像URLを一括で保存したいです。
自身のコードのどの部分が '欲しい画像のURL' に入れる部分かも分からないので教えていただきたいです。
下記のサイトを参考にしています。
①http://arduinopid.web.fc2.com/R19.html
②https://su-gi-rx.com/archives/976
現在書いているコードは以下のようになります。
import requests import pandas as pd import json # あらかじめID等は取得しておく instragramID = "0123456789" ACCESS_TOKEN = "ACCESS_TOKEN" # 検索したいワード query = "Castelvecchio" id_search_url = "https://graph.facebook.com/ig_hashtag_search?user_id=" + instragramID + "&q=" + query + "&access_token=" + ACCESS_TOKEN response = requests.get(id_search_url) hash_id = response.json() print(hash_id) # 検索タイプを選択 serch_type = "top_media" url = "https://graph.facebook.com/" + "17843667142027510" + "/" + serch_type + "?user_id=" + instragramID + "&q=" + query + "&access_token=" + ACCESS_TOKEN + "&fields=caption,media_type,media_url,children{media_url}&limit=50" response = requests.get(url) json_data = response.json() for b in range(3): url=json_data["paging"]["next"] response = requests.get(url) json_data = response.json() print("======================================") print("======================================") print("======================================") print("======================================") print("======================================") for i in range(len(json_data['data'])): #print(json_data['data'][i]) print(b*50+i+1) print("=======================================") if 'children' in json_data['data'][i]: #print(json_data["data"][i])#[”data”]っていうのは、取得した項目の名前(children,url...) for a in range(len(json_data["data"][i]['children']['data'])): print(json_data["data"][i]['children']['data'][a]['media_url']) else: print(json_data["data"][i]['media_url']) print("Wwwwwwwwwwwwwwwwwwwwwwwwwwwwww") print (json_data["paging"]) info = json_data df2 = pd.json_normalize(info['data']) print(df2) df2.to_csv(r"Castelvecchio.csv", mode = 'a',encoding='utf-8-sig') #①のサイトを参照。そのままコードを張り付けると指定画像2枚をデスクトップ上に保存できるが、任意のフォルダに入れる方法が分かりません。 import requests from bs4 import BeautifulSoup img_list = [] url = 'http://arduinopid.web.fc2.com/test.html' # 任意のurlを指定 url_cont = BeautifulSoup(requests.get(url).content,'lxml') # url解析 img_all = url_cont.find_all("img") # imgタグ情報を取得 for d in img_all: # imgタグ情報を一つずつ抽出 d = d.get("src") # src情報を取得 if d.startswith("http") and (d.endswith("jpg") or d.endswith("png")): img_list.append(d) # srcの末尾が.jpgか.pngの場合リストに追加 for img_data in img_list: # 画像データをファイルに保存 with open(img_data.split('/')[-1], 'wb') as f: f.write(requests.get(img_data).content) # ファイル保存 print(img_data.split('/')[-1]) # 保存ファイル名出力 #②を参照。with open('img/'~の img/ 部分を任意のフォルダパスを入れるも、Python上でokのみがプリントされるだけで、画像は1枚も保存されません。 import requests # urlを読み込むためrequestsをインポート from bs4 import BeautifulSoup # htmlを読み込むためBeautifulSoupをインポート URL = 'https://su-gi-rx.com' # URL入力 images = [] # 画像リストの配列 soup = BeautifulSoup(requests.get(URL).content,'lxml') # bsでURL内を解析 for link in soup.find_all("img"): # imgタグを取得しlinkに格納 if link.get("src").endswith(".jpg"): # imgタグ内の.jpgであるsrcタグを取得 images.append(link.get("src")) # imagesリストに格納 elif link.get("src").endswith(".png"): # imgタグ内の.pngであるsrcタグを取得 images.append(link.get("src")) # imagesリストに格納 for target in images: # imagesからtargetに入れる re = requests.get(target) with open("img/" + target.split('/')[-1], 'wb') as f: # imgフォルダに格納 f.write(re.content) # .contentにて画像データとして書き込む print("ok") # 確認
回答1件
あなたの回答
tips
プレビュー