Last active
September 27, 2023 08:06
-
-
Save clarissarjtai/850bf17c95a3648a4f8e43207466bb09 to your computer and use it in GitHub Desktop.
Getting all image and video urls from an Instagram post.
This file contains hidden or 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 time | |
from selenium import webdriver | |
from bs4 import BeautifulSoup as Soup | |
from selenium.webdriver.common.by import By | |
from selenium.webdriver.support.wait import WebDriverWait | |
from selenium.webdriver.support import expected_conditions as EC | |
media_url = [] | |
count = 0 # 目前獲得圖片連結數 | |
soup = Soup(browser.page_source,"lxml") | |
WebDriverWait(browser, 15).until(EC.presence_of_element_located((By.CLASS_NAME, 'ltEKP'))) | |
if (soup.find(class_="Ckrof") != None): | |
button = 1 | |
while(button != None): # 如果下一頁按鈕存在 | |
soup = Soup(browser.page_source,"lxml") | |
time.sleep(1) | |
# 取得網頁元素框架 | |
img_frame = soup.find_all(class_="Ckrof") | |
vid_frame = soup.find_all(class_="_5wCQW") | |
# 找圖片連結 | |
for i in img_frame: | |
try: | |
new_img = i.img.get('src') | |
if (new_img != None) & (new_img not in media_url): | |
media_url.append(new_img) | |
count += 1 | |
except: | |
pass | |
# 找影片連結 | |
for j in vid_frame: | |
try: | |
new_vid = j.video.get("src") | |
if (new_vid != None) & (new_vid not in media_url): | |
media_url.append(new_vid) | |
count += 1 | |
except: | |
pass | |
# 尋找下一頁按紐 | |
try: | |
WebDriverWait(browser, 5).until(EC.presence_of_element_located((By.CLASS_NAME, '_6CZji'))) | |
button = browser.find_elements_by_class_name('_6CZji')[0] | |
button.click() | |
except: | |
button = None | |
else: # 如果有沒有下一頁(單張圖片或影片) | |
# 取得網頁元素框架 | |
soup = Soup(browser.page_source,"lxml") | |
img_frame = soup.find(class_="KL4Bh") | |
vid_frame = soup.find(class_="_5wCQW") | |
# 取得圖片連結 | |
try: | |
new_img = img_frame.img.get('src') | |
if (new_img != None): | |
media_url.append(new_img) | |
count += 1 | |
except: | |
pass | |
# 取得影片連結 | |
try: | |
new_vid = vid_frame.video.get('src') | |
if (new_vid != None): | |
media_url.append(new_vid) | |
count += 1 | |
except: | |
pass | |
print("> get all media links: " + str(count) + " in total") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment