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

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

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

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

Q&A

1回答

1482閲覧

インスタのJSONデータが呼び込めない

SoojunBarng

総合スコア10

Python

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

0グッド

0クリップ

投稿2020/05/14 15:49

前提・実現したいこと

インスタグラムのデータを呼び込みたい

■■な機能を実装中に以下のエラーメッセージが発生しました。

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

data = pattern.search(script.text).group(1) AttributeError: 'NoneType' object has no attribute 'group'

該当のソースコード

python

1 2from bs4 import BeautifulSoup 3from datetime import datetime 4import sys 5import requests 6import json 7import time 8import openpyxl 9import excel 10import csv 11import re 12 13sleep_timer = 1 14 15def get_json_data(url): 16 17 res = requests.get(url) 18 html = BeautifulSoup(res.content, 'html.parser') 19 20 pattern = re.compile("window._sharedData") 21 script = html.find("script", text=pattern) 22 data = pattern.search(script.text).group(1) 23 json_user_data = json.loads(data) 24 25 return(json_user_data) 26 27def get_hashtag_stats(tags): 28 29 for tag in tags: 30 31 json_hashtag = get_json_data('https://www.instagram.com/explore/tags/'+str(tag)+'/') 32 33 number_of_posts = json_hashtag['entry_data']['TagPage'][0]['graphql']['hashtag']['edge_hashtag_to_media']['count'] 34 timestamp_1st = json_hashtag['entry_data']['TagPage'][0]['graphql']['hashtag']['edge_hashtag_to_media']['edges'][0]['node']['taken_at_timestamp'] 35 timestamp_1st = datetime.fromtimestamp(timestamp_1st) 36 timestamp_10th = json_hashtag['entry_data']['TagPage'][0]['graphql']['hashtag']['edge_hashtag_to_media']['edges'][9]['node']['taken_at_timestamp'] 37 timestamp_10th = datetime.fromtimestamp(timestamp_10th) 38 39 #最新の投稿と10個前の投稿の時間の差を求める 40 diff = timestamp_1st - timestamp_10th 41 42 post_freq= int(diff.total_seconds()/(10*60)) 43 44 output_line = [datetime.now().strftime("%d/%m/%Y %H:%M:%S"), str(tag), str(number_of_posts), str(post_freq)] 45 print(output_line) 46 47 with open(r'./hashtags_stats.csv', 'a') as f: 48 writer = csv.writer(f) 49 writer.writerow(output_line) 50 51 if sleep_timer != 0: 52 time.sleep(sleep_timer) 53 54 return() 55 56def clean_hashtags(tags): 57 58 if tags[0] == '#': 59 60 tags = tags.replace(' ', '') 61 hash_array = tags.split('#') 62 63 else: 64 65 hash_array = tags.split(',') 66 67 taglist = [a for a in hash_array if a != ''] 68 69 return(taglist) 70 71 72if __name__ == "__main__": 73 74 hashtags = "#LondonNature #britain #centrallondon #england #explorelondon #greatbritain #ig_london #igerslondon #ilovelondon #ldn #london #london4all #london_masters #london_only #londonbest #londoncity #londoner #londonlife #londonlover #londontown #londonuk #londonview #lovegreatbritain #prettycitylondon #prettylittlelondon #shutup_london #thisislondon #uk #unitedkingdom #visitlondon #visituk" 75 get_hashtag_stats(clean_hashtags(hashtags)) 76 77

試したこと

groupもなくしたし、
そもそも、dataをプリントしても何もデータ読み込めない

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

ここにより詳細な情報を記載してください。

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

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

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

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

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

guest

回答1

0

スクレイピングで何が返ってくるかはコードだけでは判断できないので、順を追って確認していくしか無いかと思います。

pattern.search(script.text)がNoneを返すということはscriptが空の可能性があるので、その前にprint(script)を入れてみるとか。
想定値が入っていなければ、その前のhtml.find("script", text=pattern)が正しいか調べてみる、とかです。

投稿2020/05/15 01:43

x98000

総合スコア1096

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

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

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

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問