2
2

More than 1 year has passed since last update.

初心者がinstagramの自動いいね作ってみた

Last updated at Posted at 2022-06-12

概要

pythonを触り始めて1ヶ月ほど経ったのでアウトプットをしてみる

流れ

インスタへの自動ログイン

タグのページへ移動

最新の投稿のパス?を取得

いいね、次の投稿へのスワイプを実行
(ループを回す)

必要なライブラリのインポート

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
import time

 自動ブラウザの起動

browser = webdriver.Chrome()
url = 'https://www.instagram.com/explore/'
browser.get(url)
time.sleep(3)
keyword_tag = 'タグ名'
username = 'ユーザーネーム'
password = 'パスワード'
num_nice = 100

ログイン処理

elem_username = browser.find_element_by_name('username')
elem_username.send_keys(username)
elem_password = browser.find_element_by_name("password")
elem_password.send_keys(password)
elem_botton = browser.find_element_by_xpath('/html/body/div[1]/section/main/div/div/div[1]/div[2]/form/div/div[3]')
time.sleep(1)
elem_botton.click()
time.sleep(3)

指定したタグページに移動

def function_search(keyword_tag):
    browser.get( url +"tags/"+ keyword_tag)
    time.sleep(3)
function_search(keyword_tag)
time.sleep(5)

最初の投稿へ焦点を合わせる

elem_first_target =browser.find_element_by_xpath('/html/body/div[1]/div/div[1]/div/div[1]/div/div/div[1]/div[1]/section/main/article/div[2]/div/div[1]/div[1]')
elem_first_target.click()
nice_num = 100
time.sleep(3)

投稿へのいいねと次の投稿への移動


for i in range(nice_num-1):
    elem_target = browser.find_element_by_class_name('_aamw').click()
    time.sleep(3)
    elem_next = browser.find_element_by_xpath('/html/body/div[1]/div/div[1]/div/div[2]/div/div/div[1]/div/div[3]/div/div/div/div/div[1]/div/div/div[2]/button').click()
    time.sleep(5)

感想

初めてカタチになるものを作ってみて、さらにプログラミングへの興味が増した。しかし、今後どのような学習をしていけばいいか曖昧な状況であり、学生のうちに就職可能な実力をつけたいところです。 それぞれの処理を関数でまとめたいのだが、browserが定義されていないとエラーを吐くので諦めた。
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2