喜欢使用Selenium的 Instagram 图片 | Python
在本文中,我们将学习如何在不滚动和手动单击按钮的情况下喜欢 Instagram 上个人资料的所有图片。我们将使用Selenium来完成这项任务。
所需的软件包/软件:
1. Python 3
2. Chromedriver compatible with the existing chrome version (download chromedriver)
3. Google chrome
4. Selenium package (pip install selenium), bs4 package(pip install bs4)
步骤#1:导入模块并输入登录信息以及页面的 URL。
Python3
from bs4 import BeautifulSoup as bs
import selenium.common.exceptions
from selenium import webdriver
import time
from selenium.webdriver.common.keys import Keys
print("enter username")
username = input()
print("enter password")
password = input()
print("enter the url")
url = input()
Python3
def path():
global chrome
print("enter the driver path")
exe_path = input()
# starts a new chrome session
chrome = webdriver.Chrome(executable_path = exe_path)
Python3
def url_name(url):
# the web page opens up
chrome.get(url)
# webdriver will wait for 4 sec before throwing a
# NoSuchElement exception so that the element
# is detected and not skipped.
time.sleep(4)
Python3
def login(username, your_password):
# finds the login button
log_but = chrome.find_element_by_class_name("L3NKy")
time.sleep(2)
# clicks the login button
log_but.click()
time.sleep(4)
# finds the username box
usern = chrome.find_element_by_name("username")
# sends the entered username
usern.send_keys(username)
# finds the password box
passw = chrome.find_element_by_name("password")
# sends the entered password
passw.send_keys(your_password)
passw.send_keys(Keys.RETURN)
time.sleep(6)
notn = chrome.find_element_by_class_name("yWX7d")# dont save info button
notn.click()# click don't save button
time.sleep(3)
Python3
def first_picture():
# finds the first picture
pic = chrome.find_element_by_class_name("kIKUG")
pic.click() # clicks on the first picture
Python3
def like_pic():
time.sleep(2)
like = chrome.find_element_by_class_name('fr66n')
soup = bs(like.get_attribute('innerHTML'),'html.parser')
if(soup.find('svg')['aria-label'] == 'Like'):
like.click()
time.sleep(2)
Python3
def next_picture():
time.sleep(2)
try:
nex = chrome.find_element_by_class_name("coreSpriteRightPaginationArrow")
time.sleep(1)
return nex
except selenium.common.exceptions.NoSuchElementException:
return 0
Python3
def continue_liking():
while(True):
next_el = next_picture()
# if next button is there then
if next_el != False:
# click the next button
next_el.click()
time.sleep(2)
# like the picture
like_pic()
time.sleep(2)
else:
print("not found")
break
Python3
path()
time.sleep(1)
url_name(url)
login(username, password)
first_picture()
like_pic()
continue_liking()
chrome.close()
步骤#2:输入系统中chromedriver.exe文件所在路径的函数
Python3
def path():
global chrome
print("enter the driver path")
exe_path = input()
# starts a new chrome session
chrome = webdriver.Chrome(executable_path = exe_path)
步骤#3:输入页面URL的函数
Python3
def url_name(url):
# the web page opens up
chrome.get(url)
# webdriver will wait for 4 sec before throwing a
# NoSuchElement exception so that the element
# is detected and not skipped.
time.sleep(4)
步骤#4:输入登录信息的函数
Python3
def login(username, your_password):
# finds the login button
log_but = chrome.find_element_by_class_name("L3NKy")
time.sleep(2)
# clicks the login button
log_but.click()
time.sleep(4)
# finds the username box
usern = chrome.find_element_by_name("username")
# sends the entered username
usern.send_keys(username)
# finds the password box
passw = chrome.find_element_by_name("password")
# sends the entered password
passw.send_keys(your_password)
passw.send_keys(Keys.RETURN)
time.sleep(6)
notn = chrome.find_element_by_class_name("yWX7d")# dont save info button
notn.click()# click don't save button
time.sleep(3)
步骤#5:打开第一张图片的函数
Python3
def first_picture():
# finds the first picture
pic = chrome.find_element_by_class_name("kIKUG")
pic.click() # clicks on the first picture
步骤#6:点赞图片的函数
Python3
def like_pic():
time.sleep(2)
like = chrome.find_element_by_class_name('fr66n')
soup = bs(like.get_attribute('innerHTML'),'html.parser')
if(soup.find('svg')['aria-label'] == 'Like'):
like.click()
time.sleep(2)
步骤#7:单击下一步按钮的函数
Python3
def next_picture():
time.sleep(2)
try:
nex = chrome.find_element_by_class_name("coreSpriteRightPaginationArrow")
time.sleep(1)
return nex
except selenium.common.exceptions.NoSuchElementException:
return 0
步骤#8:继续喜欢图片直到找不到下一个按钮的函数
Python3
def continue_liking():
while(True):
next_el = next_picture()
# if next button is there then
if next_el != False:
# click the next button
next_el.click()
time.sleep(2)
# like the picture
like_pic()
time.sleep(2)
else:
print("not found")
break
步骤#9:调用函数
Python3
path()
time.sleep(1)
url_name(url)
login(username, password)
first_picture()
like_pic()
continue_liking()
chrome.close()
给你!该脚本将自动喜欢所有帖子,直到页面结束。