我写了一个简单的登录脚本尝试从服务器登录到Instagram
import requests
import pprint
INSTAGRAM_URL = 'https://www.instagram.com'
USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36'
IG_ACCOUNTS = [
{'username': 'username1', 'password': 'pass1'},
{'username': 'username2', 'password': 'pass2'},
]
def login(username, password):
login_url = INSTAGRAM_URL + '/accounts/login/ajax/'
session = requests.Session()
session.headers = { 'user-agent': USER_AGENT }
session.headers.update({'Referer': INSTAGRAM_URL})
req = session.get(INSTAGRAM_URL)
session.headers.update({'X-CSRFToken': req.cookies['csrftoken']})
login = session.post(login_url, data={'username': username, 'password': password}, allow_redirects=True)
print('login: ')
pprint.pprint(login)
session.headers.update({'X-CSRFToken': login.cookies['csrftoken']})
cookies = login.cookies
results = login.json()
print('login results')
pprint.pprint(results)
if (results['authenticated'] == False):
return False, None
return True, cookies
login(IG_ACCOUNTS[0]['username'], IG_ACCOUNTS[0]['password'])
但是我收到了错误
HTTP status: 200
{
'error_type': 'generic_request_error',
'errors': {'error': ['Sorry, there was a problem with your request.']},
'status': 'ok'
}
但是当我从本地机器运行这个脚本时,它运行得很好。
{
'user': True,
'status': 'ok',
'userId': '12345678910',
'authenticated': True,
'reactivated': True
}
我注意到Instagram阻止了IP地址,即使我尝试使用Tor browser登录,我也会遇到同样的错误。
有任何建议如何解决这个问题?