我正在开发一个简单的项目来改进Python。我正在使用请求库来获取“https://www.instagram.com/graphql/query/?query_hash=58712303d941c6855d4e888c5f0cd22f&variables=%7B%22id%22%3A%2225025320%22%2C%22first%22%3A24%7D”,这是在Instagram的以下(https://www.instagram.com/instagram/following/)上单击以下内容时加载的第一个x跟随量。我的问题是,我如何解析以下所有内容?我尝试在线搜索,找不到任何演示如何连续获取下一个query_hash网址的结果。这是我目前的代码:
# Library imports
import requests
import json
import time
# Variables
LOGIN_URL = 'https://www.instagram.com/accounts/login/ajax/'
REFERER_URL = 'https://www.instagram.com/accounts/login/'
USER_AGENT = 'Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1'
USERNAME = 'username'
PASSWD = 'password'
IGQ = r"https://www.instagram.com/graphql/query/?query_hash=58712303d941c6855d4e888c5f0cd22f&variables=%7B%22id%22%3A%2225025320%22%2C%22first%22%3A24%7D"
# Session variables
session = requests.Session()
req = session.get(LOGIN_URL)
session.headers = {'user-agent': USER_AGENT}
session.headers.update({'Referer': REFERER_URL})
session.headers = {'user-agent': USER_AGENT}
session.headers.update({'x-csrftoken': req.cookies['csrftoken']})
login_data = {'username': USERNAME, 'password': PASSWD}
login = session.post(LOGIN_URL, data=login_data, allow_redirects=True)
session.headers.update({'x-csrftoken': login.cookies['csrftoken']})
# Parse followings
def parse():
try:
following = session.get(IGQ)
test_text = json.loads(following.text)
usernames = []
j = test_text['data']['user']['edge_follow']
for each in j['edges']:
usernames.append(each['node']['username'])
print(usernames)
except:
print("Couldn't login.")
parse()
我目前可以成功解析前x个以下数量,但我不确定如何解析其余的。在chrome dev工具上,滚动时的下一个请求是:qazxsw poi
这是我正在使用的json,第一个url的响应:
https://www.instagram.com/graphql/query/?query_hash=58712303d941c6855d4e888c5f0cd22f&variables=%7B%22id%22%3A%2225025320%22%2C%22first%22%3A12%2C%22after%22%3A%22AQB-48qzOZue7n4BHPi7FETk2TQnrPl5LiWJKl2nsPCUkLcralRpeTo6F3zQze71zjKh7iDypwv4yxR6OOyHwYj-r1hU5S-P1QaMlRn59i3emA%22%7D
所以我基本上要做的是在以下所有内容中获取所有json响应。我不确定如何解决这个问题,非常感谢任何帮助。
在响应中有一个名为“end_cursor”的键。使用end_cursor进行分页。
用适当的键替换end_cursor。您可以在第一个请求中将end_cursor留空。
{data: {user: {edge_follow: {count: 193, page_info: {has_next_page: true,…},…}}}, status: "ok"}
data
:
{user: {edge_follow: {count: 193, page_info: {has_next_page: true,…},…}}}
user
:
{edge_follow: {count: 193, page_info: {has_next_page: true,…},…}}
edge_follow
:
{count: 193, page_info: {has_next_page: true,…},…}
count
:
193
edges
:
[{node: {id: "1298763699", username: "mrbentley_thedog", full_name: "Mister Bentley",…}},…]
0
:
{node: {id: "1298763699", username: "mrbentley_thedog", full_name: "Mister Bentley",…}}
1
:
{node: {id: "28892894", username: "guskenworthy", full_name: "gus kenworthy",…}}
2
:
{node: {id: "26633036", username: "anitta", full_name: "anitta 🎤",…}}
3
:
{node: {id: "433479649", username: "puffytails", full_name: "Puffytails Trio",…}}
4
:
{node: {id: "6106847", username: "ttlyteala", full_name: "Teala Dunn",…}}
5
:
{node: {id: "10766410", username: "wrenees", full_name: "Renee Lusano",…}}
6
:
{node: {id: "18428658", username: "kimkardashian", full_name: "Kim Kardashian West",…}}
7
:
{node: {id: "320996985", username: "jugglinjosh", full_name: "Josh Horton",…}}
8
:
{node: {id: "177402262", username: "lelepons", full_name: "Lele Pons",…}}
9
:
{node: {id: "1390031219", username: "brycexavier", full_name: "bryce xavier",…}}
10
:
{node: {id: "1081938380", username: "katieaustin", full_name: "Katie Austin",…}}
11
:
{node: {id: "284216174", username: "susiemeoww", full_name: "Susie Shu 🍒",…}}
12
:
{node: {id: "2786948", username: "theshoesurgeon", full_name: "Dominic Chambrone",…}}
13
:
{node: {id: "182973434", username: "laurengodwin", full_name: "lauren godwin🧚🏼♀️✨",…}}
14
:
{node: {id: "16911665", username: "laurdiy", full_name: "Lauren Riihimaki",…}}
15
:
{node: {id: "2077685663", username: "ninja", full_name: "Tyler Blevins",…}}
16
:
{node: {id: "1194735637", username: "mannymua733", full_name: "🌙Manny Gutierrez",…}}
17
:
{node: {id: "5603022012", username: "wildspotted", full_name: "Ida",…}}
18
:
{node: {id: "32085887", username: "lisafreestyle", full_name: "Lisa Zimouche",…}}
19
:
{node: {id: "241302041", username: "kiliiiyuyan", full_name: "Kiliii Yuyan",…}}
20
:
{node: {id: "496200129", username: "ts_abe", full_name: "T.S ABE",…}}
21
:
{node: {id: "424605784", username: "laetitiaky", full_name: "KY",…}}
22
:
{node: {id: "2516489", username: "michaelwalchalk", full_name: "Michael Walchalk",…}}
23
:
{node: {id: "3833293301", username: "afrosinsanjuan", full_name: "Photographing Afro Caribbeans",…}}
page_info
:
{has_next_page: true,…}
end_cursor
:
"AQB-48qzOZue7n4BHPi7FETk2TQnrPl5LiWJKl2nsPCUkLcralRpeTo6F3zQze71zjKh7iDypwv4yxR6OOyHwYj-r1hU5S-P1QaMlRn59i3emA"
has_next_page
:
true
status
:
"ok"