Laden Sie Instagram-Beiträge mit dem Python Selenium-Modul herunter
In diesem Artikel erfahren Sie, wie Sie mithilfe des Python Selenium- Moduls Instagram-Beiträge eines Profils herunterladen können .
Bedarf:
- Google Chrome oder Firefox
- Chrome-Treiber (für Google Chrome) oder Gecko-Treiber (für Mozilla Firefox)
- Selenium-Paket: Es ist ein leistungsstarkes Tool zur Steuerung eines Webbrowsers über das Programm. Es ist für alle Browser funktionsfähig, funktioniert unter allen gängigen Betriebssystemen und seine Skripte sind in verschiedenen Sprachen geschrieben, z. B. Python, Java, C # usw. In kann mit dem folgenden Befehl installiert werden:
Pip installieren selenium
- Wunderschönes Seifenpaket : Es ist eine Python-Bibliothek zum Abrufen von Daten aus HTML- und XML-Dateien. Es funktioniert mit Ihrem bevorzugten Parser und bietet idiomatische Möglichkeiten zum Navigieren, Suchen und Ändern des Analysebaums. Es kann mit dem folgenden Befehl installiert werden:
pip install bs4
- Anforderungspaket: Die Anforderungsbibliothek ist ein wesentlicher Bestandteil von Python, um HTTP-Anforderungen an eine angegebene URL zu senden. Es kann mit dem folgenden Befehl installiert werden:
Pip Installationsanforderungen
Schritt-für-Schritt-Ansatz:
Schritt 1: Importieren von Modulen und Eingeben der Anmeldeinformationen zusammen mit der URL der Seite.
from
selenium
import
webdriver
from
selenium.webdriver.common.keys
import
Keys
import
selenium.common.exceptions
import
time
from
bs4
import
BeautifulSoup as bs
import
requests
import
os
username
=
input
(
'Enter Your User Name '
)
password
=
input
(
'Enter Your Password '
)
url
=
'https://instagram.com/'
+
\
input
(
'Enter User Name Of User For Downloading Posts '
)
Schritt 2: Funktion zum Starten der neuen Browsersitzung. Möglicherweise müssen Sie den Pfad zum Webtreiber hinzufügen. Die Funktion Chrome() hängt von Ihrer Installation ab.
def
path():
global
chrome
chrome
=
webdriver.Chrome()
Schritt 3: Funktion zur Eingabe der URL der Seite.
def
url_name(url):
chrome.get(url)
time.sleep(
4
)
Schritt 4: Funktion zur Eingabe Ihrer Anmeldeinformationen.
def
login(username, your_password):
log_but
=
chrome.find_element_by_class_name(
"L3NKy"
)
time.sleep(
2
)
log_but.click()
time.sleep(
4
)
usern
=
chrome.find_element_by_name(
"username"
)
usern.send_keys(username)
passw
=
chrome.find_element_by_name(
"password"
)
passw.send_keys(your_password)
passw.send_keys(Keys.RETURN)
time.sleep(
5.5
)
notn
=
chrome.find_element_by_class_name(
"yWX7d"
)
notn.click()
time.sleep(
3
)
Schritt 5: Funktion zum Öffnen des ersten Beitrags.
def
first_post():
pic
=
chrome.find_element_by_class_name(
"kIKUG"
).click()
time.sleep(
2
)
Schritt 6: Funktion zum Herunterladen aller Beiträge.
def
download_allposts():
first_post()
user_name
=
url.split(
'/'
)[
-
1
]
if
(os.path.isdir(user_name)
=
=
False
):
os.mkdir(user_name)
multiple_images
=
nested_check()
if
multiple_images:
nescheck
=
multiple_images
count_img
=
0
while
nescheck:
elem_img
=
chrome.find_element_by_class_name(
'rQDP3'
)
save_multiple(user_name
+
'/'
+
'content1.'
+
str
(count_img), elem_img)
count_img
+
=
1
nescheck.click()
nescheck
=
nested_check()
save_multiple(user_name
+
'/'
+
'content1.'
+
str
(count_img), elem_img, last_img_flag
=
1
)
else
:
save_content(
'_97aPb'
, user_name
+
'/'
+
'content1'
)
c
=
2
while
(
True
):
next_el
=
next_post()
if
next_el !
=
False
:
next_el.click()
time.sleep(
1.3
)
try
:
multiple_images
=
nested_check()
if
multiple_images:
nescheck
=
multiple_images
count_img
=
0
while
nescheck:
elem_img
=
chrome.find_element_by_class_name(
'rQDP3'
)
save_multiple(user_name
+
'/'
+
'content'
+
str
(c)
+
'.'
+
str
(count_img), elem_img)
count_img
+
=
1
nescheck.click()
nescheck
=
nested_check()
save_multiple(user_name
+
'/'
+
'content'
+
str
(c)
+
'.'
+
str
(count_img), elem_img,
1
)
else
:
save_content(
'_97aPb'
, user_name
+
'/'
+
'content'
+
str
(c))
except
selenium.common.exceptions.NoSuchElementException:
(
"finished"
)
return
else
:
break
c
+
=
1
Schritt 7: Funktion zum Klicken auf den nächsten Beitrag.
def
next_post():
try
:
nex
=
chrome.find_element_by_class_name(
"coreSpriteRightPaginationArrow"
)
return
nex
except
selenium.common.exceptions.NoSuchElementException:
return
0
Schritt 8: Funktion zum Speichern normaler Beiträge.
def
save_content(class_name,img_name):
time.sleep(
0.5
)
try
:
pic
=
chrome.find_element_by_class_name(class_name)
except
selenium.common.exceptions.NoSuchElementException:
(
"Either This user has no images or you haven't followed this user or something went wrong"
)
return
html
=
pic.get_attribute(
'innerHTML'
)
soup
=
bs(html,
'html.parser'
)
link
=
soup.find(
'video'
)
if
link:
link
=
link[
'src'
]
else
:
link
=
soup.find(
'img'
)[
'src'
]
response
=
requests.get(link)
with
open
(img_name,
'wb'
) as f:
f.write(response.content)
time.sleep(
0.9
)
Schritt 9: Funktion zum Speichern verschachtelter Beiträge.
def
save_multiple(img_name,elem,last_img_flag
=
False
):
time.sleep(
1
)
l
=
elem.get_attribute(
'innerHTML'
)
html
=
bs(l,
'html.parser'
)
biglist
=
html.find_all(
'ul'
)
biglist
=
biglist[
0
]
list_images
=
biglist.find_all(
'li'
)
if
last_img_flag:
user_image
=
list_images[
-
1
]
else
:
user_image
=
list_images[(
len
(list_images)
/
/
2
)]
video
=
user_image.find(
'video'
)
if
video:
link
=
video[
'src'
]
else
:
link
=
user_image.find(
'img'
)[
'src'
]
response
=
requests.get(link)
with
open
(img_name,
'wb'
) as f:
f.write(response.content)
Schritt 10: Funktion zum Überprüfen, ob der Beitrag verschachtelt ist oder nicht.
def
nested_check():
try
:
time.sleep(
1
)
nes_nex
=
chrome.find_element_by_class_name(
'coreSpriteRightChevron '
)
return
nes_nex
except
selenium.common.exceptions.NoSuchElementException:
return
0
Schritt 11: Aufrufen der erforderlichen Funktionen im Treibercode.
path()
time.sleep(
1
)
url_name(url)
login(username, password)
download_allposts()
chrome.close()
Nachfolgend finden Sie das vollständige Programm, das auf dem oben genannten Ansatz basiert:
from
selenium
import
webdriver
from
selenium.webdriver.common.keys
import
Keys
import
selenium.common.exceptions
import
time
from
bs4
import
BeautifulSoup as bs
import
requests
import
os
username
=
input
(
'Enter Your User Name '
)
password
=
input
(
'Enter Your Password '
)
url
=
'https://instagram.com/'
+
\
input
(
'Enter User Name Of User For Downloading Posts '
)
def
path():
global
chrome
chrome
=
webdriver.Chrome()
def
url_name(url):
chrome.get(url)
time.sleep(
4
)
def
login(username, your_password):
log_but
=
chrome.find_element_by_class_name(
"L3NKy"
)
time.sleep(
2
)
log_but.click()
time.sleep(
4
)
usern
=
chrome.find_element_by_name(
"username"
)
usern.send_keys(username)
passw
=
chrome.find_element_by_name(
"password"
)
passw.send_keys(your_password)
passw.send_keys(Keys.RETURN)
time.sleep(
5.5
)
notn
=
chrome.find_element_by_class_name(
"yWX7d"
)
notn.click()
time.sleep(
3
)
def
first_post():
pic
=
chrome.find_element_by_class_name(
"kIKUG"
).click()
time.sleep(
2
)
def
next_post():
try
:
nex
=
chrome.find_element_by_class_name(
"coreSpriteRightPaginationArrow"
)
return
nex
except
selenium.common.exceptions.NoSuchElementException:
return
0
def
download_allposts():
first_post()
user_name
=
url.split(
'/'
)[
-
1
]
if
(os.path.isdir(user_name)
=
=
False
):
os.mkdir(user_name)
multiple_images
=
nested_check()
if
multiple_images:
nescheck
=
multiple_images
count_img
=
0
while
nescheck:
elem_img
=
chrome.find_element_by_class_name(
'rQDP3'
)
save_multiple(user_name
+
'/'
+
'content1.'
+
str
(count_img), elem_img)
count_img
+
=
1
nescheck.click()
nescheck
=
nested_check()
save_multiple(user_name
+
'/'
+
'content1.'
+
str
(count_img), elem_img, last_img_flag
=
1
)
else
:
save_content(
'_97aPb'
, user_name
+
'/'
+
'content1'
)
c
=
2
while
(
True
):
next_el
=
next_post()
if
next_el !
=
False
:
next_el.click()
time.sleep(
1.3
)
try
:
multiple_images
=
nested_check()
if
multiple_images:
nescheck
=
multiple_images
count_img
=
0
while
nescheck:
elem_img
=
chrome.find_element_by_class_name(
'rQDP3'
)
save_multiple(user_name
+
'/'
+
'content'
+
str
(c)
+
'.'
+
str
(count_img), elem_img)
count_img
+
=
1
nescheck.click()
nescheck
=
nested_check()
save_multiple(user_name
+
'/'
+
'content'
+
str
(c)
+
'.'
+
str
(count_img), elem_img,
1
)
else
:
save_content(
'_97aPb'
, user_name
+
'/'
+
'content'
+
str
(c))
except
selenium.common.exceptions.NoSuchElementException:
(
"finished"
)
return
else
:
break
c
+
=
1
def
save_content(class_name, img_name):
time.sleep(
0.5
)
try
:
pic
=
chrome.find_element_by_class_name(class_name)
except
selenium.common.exceptions.NoSuchElementException:
(
"Either This user has no images or you haven't followed this user or something went wrong"
)
return
html
=
pic.get_attribute(
'innerHTML'
)
soup
=
bs(html,
'html.parser'
)
link
=
soup.find(
'video'
)
if
link:
link
=
link[
'src'
]
else
:
link
=
soup.find(
'img'
)[
'src'
]
response
=
requests.get(link)
with
open
(img_name,
'wb'
) as f:
f.write(response.content)
time.sleep(
0.9
)
def
save_multiple(img_name, elem, last_img_flag
=
False
):
time.sleep(
1
)
l
=
elem.get_attribute(
'innerHTML'
)
html
=
bs(l,
'html.parser'
)
biglist
=
html.find_all(
'ul'
)
biglist
=
biglist[
0
]
list_images
=
biglist.find_all(
'li'
)
if
last_img_flag:
user_image
=
list_images[
-
1
]
else
:
user_image
=
list_images[(
len
(list_images)
/
/
2
)]
video
=
user_image.find(
'video'
)
if
video:
link
=
video[
'src'
]
else
:
link
=
user_image.find(
'img'
)[
'src'
]
response
=
requests.get(link)
with
open
(img_name,
'wb'
) as f:
f.write(response.content)
def
nested_check():
try
:
time.sleep(
1
)
nes_nex
=
chrome.find_element_by_class_name(
'coreSpriteRightChevron '
)
return
nes_nex
except
selenium.common.exceptions.NoSuchElementException:
return
0
path()
time.sleep(
1
)
url_name(url)
login(username, password)
download_allposts()
chrome.close()
Nach dem Ausführen dieses vollständigen Skripts wird ein Verzeichnis erstellt, das alle Beiträge enthält.
Ausgabe:
Hinweis: Wenn Sie Windows verwenden, werden die Beiträge mit der Erweiterung .file gespeichert . Öffnen Sie die Beiträge mit einer Anwendung, mit der sowohl Bilder als auch Videos geöffnet werden können (Instagram-Beiträge enthalten nur Medien, Bilder oder Videos).