質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

0回答

919閲覧

Instagramのコード実行をしたいのですができません。。初心者なのでお教えいただけると嬉しいです。

y.m711

総合スコア0

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2021/03/21 03:23

前提・実現したいこと

こちらのコードを実行したのですが、エラー表示が出てしまい治し方を教えていただきたいです。

発生している問題・エラーメッセージ

raceback (most recent call last): File "app.py", line 45, in <module> driver = webdriver.Chrome() File "/Users/Yuito/.pyenv/versions/3.8.2/lib/python3.8/site-packages/selenium/webdriver/chrome/webdriver.py", line 73, in __init__ self.service.start() File "/Users/Yuito/.pyenv/versions/3.8.2/lib/python3.8/site-packages/selenium/webdriver/common/service.py", line 98, in start self.assert_process_still_running() File "/Users/Yuito/.pyenv/versions/3.8.2/lib/python3.8/site-packages/selenium/webdriver/common/service.py", line 109, in assert_process_still_running raise WebDriverException( selenium.common.exceptions.WebDriverException: Message: Service chromedriver unexpectedly exited. Status code was: -9

該当のソースコード

#!/usr/bin/env python # -*- coding: utf-8 -*- # 必要なライブラリのインポート from selenium import webdriver from selenium.webdriver.common.keys import Keys import urllib.parse import time import datetime import sys import traceback import readfile args = sys.argv#コマンドの引数 ####################設定#################### #1日にいいね!できる最大値。この数を超えたら処理終了 max_limit_likes_counter = 1000 #自動いいね!時、エラーがこの数を超えたら処理終了 max_limit_error_cnt = 3 #Instagramログイン用 ID PASS username = "aa" password = "aa" #読み込みファイル名 file_words = "words_" + str(args[1]) +".txt" file_l_cnt = "likes_cnt.txt" file_alu = "already_likes_url.txt" #################いいね!したいワードをファイルから取得 words = readfile.readWords( file_words ) #############本日、いいね!している数をファイルから取得 today = datetime.date.today() today = str(today) likes_cnt,data_other_than_today = readfile.getLikesCntToday(today,file_l_cnt) ######################すでにいいね!したURLの読み込み already_likes_url = readfile.readAlreadyLikesURL(file_alu) #Chromeを起動 driver = webdriver.Chrome() #Instagram ログインURL login_url = "https://www.instagram.com/" #ログインボタン #login_path = '//*[@id="react-root"]/section/main/article/div[2]/div[2]/p/a' #ログイン用フォームへのパス username_path = '//form//div[1]//input' password_path = '//form//div[2]//input' #いいね!ボタン取得用 like_x_path = '//main//section//button' #Instagramのサイトを開く driver.get(login_url) time.sleep(3) #ログインページへ ( Webページの構造が変化しており、↓の処理はいらなくなった。2020.04.04 確認 ) #driver.find_element_by_xpath(login_path).click() #time.sleep(3) #ユーザー名とパスワードを入力してリターンキーを押す usernameField = driver.find_element_by_xpath(username_path) usernameField.send_keys(username) time.sleep(1) passwordField = driver.find_element_by_xpath(password_path) passwordField.send_keys(password) passwordField.send_keys(Keys.RETURN) time.sleep(3) if "アカウントが不正使用されました" in driver.page_source: print(driver.page_source) print("ブロックされました。パスワードを変更する必要があります。") print("処理を終了します。") exit() #####################ハッシュタグ毎のループ #ハッシュタグ検索用のURL tag_search_url = "https://www.instagram.com/explore/tags/{}/?hl=ja" off = False#処理を終了する切り替えスイッチ error_cnt = 0 for word in words: if off: break print("http req get hashtag page: " + tag_search_url.format(word)) driver.get(tag_search_url.format(word)) time.sleep(5) driver.implicitly_wait(15) #リンクのhref属性の値を取得 mediaList = driver.find_elements_by_tag_name("a") hrefList = [] #1つのハッシュタグに表示された画像のhrefを配列に格納 for media in mediaList: href = media.get_attribute("href") if "/p/" in href: hrefList.append(href) #画像のhrefを格納した配列でループ処理 for href in hrefList: #すでにいいね!していた場合はスルー if href in already_likes_url: print("[いいね!済]" + href) else: time.sleep(2) driver.get(href) try: driver.find_element_by_xpath(like_x_path).click() time.sleep(2) #print(driver.page_source) if "ブロックされています" in driver.page_source: print("ブロックされました。処理を終了します。") off = True break likes_cnt += 1 print('いいね! {}'.format(likes_cnt)) flc = open(file_l_cnt,'w') flc.write(data_other_than_today + today + '\t' + str(likes_cnt) + '\n') flc.close() fa = open(file_alu,'a') fa.write(href + '\n') fa.close() #この地点を通過する時にいいね!300回超えてたら終了 #BAN防止 if likes_cnt >= max_limit_likes_counter: print("いいね!の上限回数({})を超えました。処理を終了します。".format(max_limit_likes_counter)) off = True except Exception as e: ex, ms, tb = sys.exc_info() print(ex) print(ms) traceback.print_tb(tb) error_cnt += 1 time.sleep(5) if error_cnt > max_limit_error_cnt: print("エラーが{}回を超えました。処理を終了します。".format(max_limit_error_cnt)) off = True if off: break print("本日のいいね!回数 {}".format(likes_cnt)) #ブラウザを閉じる #driver.quit()

試したこと

実行

補足情報(FW/ツールのバージョンなど)

chrome driverは最新版へ更新いたしました。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

meg_

2021/03/21 04:40

Instagramはスクレイピング禁止だったと思いますが、規約の変更があったのでしょうか?
CHERRY

2021/03/21 12:23

今まで動いていたのでしょうか? それとも最初から動かないのでしょうか? また、ご自身で作成されたコードですか? ご自身で書かれたコードでない場合は、作成された方に問い合わせるのが良いのではないかと思います。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問