Created
August 25, 2018 17:04
-
-
Save JuanjoSalvador/35d508f89868b64b275c1be40bac948e to your computer and use it in GitHub Desktop.
Modification of Instaget package to download pictures from a hashtag
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import json | |
import requests | |
from urllib import request | |
from bs4 import BeautifulSoup | |
def main(): | |
try: | |
url = sys.argv[1] | |
page = BeautifulSoup(requests.get(url).text, 'html.parser') | |
image = page.find_all('script') | |
image = image[3].text.replace("window._sharedData = ", "") | |
image = image.replace(";", "") | |
pictures = json.loads(image)['entry_data']['TagPage'][0]['graphql']['hashtag']['edge_hashtag_to_media']['edges'] | |
for pic in pictures: | |
image_uri = pic['node']['display_url'] | |
image_file = image_uri.split("/")[-1] | |
request.urlretrieve(image_uri, image_file) | |
except IndexError: | |
print("No URL!") | |
except ValueError as VE: | |
print("Invalid URL") | |
except requests.exceptions.MissingSchema as MissingSchema: | |
print("Invalid URL") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment