每个人。 我尝试为Instagram构建一个机器人(仅用于请求)。现在,我可以登录,但是我不喜欢该帖子。这是代码:
import re
import requests
from bs4 import BeautifulSoup
import json , pickle
from datetime import datetime
import os
from time import sleep
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
link = 'https://www.instagram.com/accounts/login/'
login_url = 'https://www.instagram.com/accounts/login/ajax/'
proxies = {
'socks': '104.248.63.17:30588'
}
time = int(datetime.now().timestamp())
jar1, jar2 = "",""
payload = {
'username': '',
'enc_password': f'#PWD_INSTAGRAM_BROWSER:0:{time}:',
'queryParams': {},
'optIntoOneTap': 'false'
}
with requests.Session() as s:
if os.path.isfile("cookies.txt"):
print("1")
with open("cookies.txt", 'r') as f:
# cookies = pickle.load(f)
# jar = requests.cookies.RequestsCookieJar()
# jar._cookies = cookies
# s.cookies = jar
s.cookies = requests.utils.cookiejar_from_dict(json.load(f))
jar = requests.cookies.RequestsCookieJar()
jar.set('tasty_cookie', 'yum', domain='httpbin.org', path='/cookies')
jar1 = jar
r = s.get(link, cookies=jar, verify=False)
sleep(1)
r = s.get("https://www.instagram.com/")
# with open("cookies2.txt", 'wb') as f:
# f.truncate()
# print(r.cookies._cookies)
# pickle.dump(r.cookies._cookies, f)
# #json.dump(requests.utils.dict_from_cookiejar(r.cookies), f)
# sleep(1)
# print(jar2)
else:
print("2")
r = s.get(link,verify=False)
sleep(2)
csrf = re.findall(r"csrf_token\":\"(.*?)\"", r.text)[0]
r = s.post(login_url, data=payload, headers={
"User-Agent": "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36",
"X-Requested-With": "XMLHttpRequest",
"Referer": "https://www.instagram.com/accounts/login/",
"x-csrftoken": csrf
},verify=False)
print(r.status_code, r.cookies)
with open("cookies.txt", 'w') as f:
# f.truncate()
# pickle.dump(r.cookies._cookies, f)
json.dump(requests.utils.dict_from_cookiejar(r.cookies), f)
sleep(1)
with open("cookies.txt", 'r') as f:
# cookies = pickle.load(f)
# jar2 = requests.cookies.RequestsCookieJar()
# print(cookies)
# jar2._cookies = cookies
# s.cookies = jar2
s.cookies = requests.utils.cookiejar_from_dict(json.load(f))
jar2 = requests.cookies.RequestsCookieJar()
jar2.set('tasty_cookie', 'yum', domain='httpbin.org', path='/cookies')
sleep(2)
# r = s.post("https://www.instagram.com/web/likes/3447/like/",cookies=jar2,headers={
# "User-Agent": "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36",
# "X-Requested-With": "XMLHttpRequest",
# "Referer": "https://www.instagram.com/accounts/login/"
# }, verify=False)
# print(r.cookies)
r = s.get("https://www.instagram.com/baklanskaiav/?__a=1")
sleep(1)
print(r)
r = s.post("https://www.instagram.com/web/friendships/39164422021/follow/")
print(r)
Instagram似乎认为我不是登录用户。
306字节是我一个人跟随的时间。 其他是我的脚本。 当我自己跟随时,我无法发送完全相同的cookie。 谢谢。