-
Notifications
You must be signed in to change notification settings - Fork 1
/
save_and_load_context.py
65 lines (46 loc) · 1.8 KB
/
save_and_load_context.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
""""This example demonstrates how to save and load the user session state using AgentQL."""
import time
import agentql
import os
import json
# Set the URL to the desired website
URL = "https://www.instagram.com"
# Set the user_id and password for the website
URL_USER_ID = "#Insert your user_id for the website here"
URL_PASSWORD = "#Insert your password for the website here"
def get_user_session_state():
# Start a session with the specified URL
session = agentql.start_session(URL)
# Define the queries to interact with the page (for login)
QUERY = """
{
username
password
login_btn
}"""
response = session.query(QUERY)
response.username.fill(URL_USER_ID)
response.password.fill(URL_PASSWORD)
response.login_btn.click(force=True)
# Wait for 5 seconds to ensure the user session state is saved entirely
time.sleep(5)
user_session = session.get_user_auth_session()
# Save the user session state to a file
with open("user_session_instagram.json", "w", encoding="utf-8") as file:
file.write(json.dumps(user_session))
session.stop()
return user_session
if __name__ == "__main__":
if os.path.exists('user_session_instagram.json'):
with open('user_session_instagram.json', 'r', encoding="utf-8") as file:
content = file.read()
user_session = json.loads(content)
else:
get_user_session_state()
with open('user_session_instagram.json', 'r', encoding="utf-8") as file:
content = file.read()
user_session = json.loads(content)
# Start a session with the specified URL
session = agentql.start_session(URL, user_auth_session=user_session)
# Wait for 5 seconds to see the browser in action
time.sleep(5)