首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用tor登录Instagram

使用tor登录Instagram
EN

Stack Overflow用户
提问于 2020-01-01 21:16:55
回答 1查看 4.9K关注 0票数 2

我正在尝试通过tor登录instagram (如果这有帮助的话,我正在linux机器上使用python3!)代码如下:

代码语言:javascript
运行
复制
import json
import requests
import os
from colorama import Fore
from stem import Signal
from stem.control import Controller

def tor_session():
session = requests.session()
session.proxies['http'] = 'socks5h://localhost:9050'
session.proxies['https'] = 'socks5h://localhost:9050'
return session

def login(username, password):
# params:
# [string]username- the username of the instagram account to log in to
# [string]password- the password to use in the log in process

# description:
# logs in to the account with the specified username and with the specified password

# session setup
sess = tor_session()
sess.cookies.update({
    'sessionid': '',
    'mid': '',
    'ig_pr': '1',
    'ig_vw': '1920',
    'csrftoken': '',
    's_network': '',
    'ds_user_id': ''
})
sess.headers.update({
    'UserAgent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36',
    'x-instagram-ajax': '1',
    'X-Requested-With': 'XMLHttpRequest',
    'origin': 'https://www.instagram.com',
    'ContentType': 'application/x-www-form-urlencoded',
    'Connection': 'keep-alive',
    'Accept': '*/*',
    'Referer': 'https://www.instagram.com',
    'authority': 'www.instagram.com',
    'Host': 'www.instagram.com',
    'Accept-Language': 'ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4',
    'Accept-Encoding': 'gzip, deflate'
})

# get csrftoken and the instagram main page
r = sess.get('https://www.instagram.com/')
sess.headers.update({'X-CSRFToken': r.cookies.get_dict()['csrftoken']})

# log in
data = {'username': username, 'password': password}
r = sess.post('https://www.instagram.com/accounts/login/ajax/', data=data, allow_redirects=True)
token = r.cookies.get_dict()['csrftoken']
sess.headers.update({'X-CSRFToken': token})

# parse the response from the log in
data = json.loads(r.text)
print(data)
if data['status'] == 'fail':
    return None
if data['authenticated']:
    return True
else:
    return False

login("username", "password")

问题是,几乎每次我尝试运行它时,它都不起作用,并抛出一个异常:

代码语言:javascript
运行
复制
Traceback (most recent call last):
File "main.py", line 156, in <module>
main()
File "main.py", line 152, in main
brute_force(username, pass_file_path)
File "main.py", line 114, in brute_force
logged_in = login(username, password)
File "main.py", line 81, in login
sess.headers.update({'X-CSRFToken': r.cookies.get_dict()['csrftoken']})
KeyError: 'csrftoken'

有时它会抛出这样的异常:

代码语言:javascript
运行
复制
File "main.py", line 94, in login
if data['authenticated']:
KeyError: 'authenticated'

我该如何解决这个问题呢?我尝试重新启动或更改其配置,但没有任何工作,请帮助,如果你可以!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-01-03 03:02:48

Instagram似乎没有为tor用户设置cookie:

代码语言:javascript
运行
复制
>>> s = your_setup_code_for_session()
>>> r = s.get('https://www.instagram.com')
>>> r.cookies.get_dict()
{}

我也使用tor浏览器对其进行了测试,并获得了相同的结果:

看起来您需要使用vpn或Tor + vpn组合。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59552867

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档