LintCode网址登陆反爬虫CSRF禁止访问

lintcode是一个非常流行的在线编程网站,今天在用python爬虫登陆进行模拟时,发现以下问题:

LintCode网址登陆反爬虫CSRF禁止访问_第1张图片

经过了解:CSRF全称:Cross-site request forgery,中文翻译为跨站请求伪造,也被称为“One Click Attack”或者Session Riding,通常缩写为CSRF或者XSRF

因此在访问时,需要拿到这个CSRF-token值,并把这个token加入消息头和cookie中一并发送出去

关键代码如下:

Token = 'KPHxDihzU8OiLudC3DsDyhVK78HO657d'
headers = {'User-Agent': agent, 'Referer': referer, 'X-CSRFToken': Token}
post_url = 'https://www.lintcode.com/accounts/signin/'
s = requests.Session()
s.keep_alive = False
requests.adapters.DEFAULT_RETRIES = 5
r = s.get(post_url, headers=headers, allow_redirects=True)
cookies = dict(__atuvc = '4|17',
                __cfduid = 'd0a8707338d055a45f8d541e4f444e6ee1524576620',
                _ga = 'GA1.2.566960194.1524577455',
                AWSALB ='k9LyEp19nKW1DLV0i084hRnrwApXexFIvpyjbDAJX3ix5VkE2qma12far5eD3foQPS/uQoZ76GdfTCznF12TyjEk31X7T9O17vNDn7+uLdKYtFRX0hNfD9YMazDb',
                csrftoken ='KPHxDihzU8OiLudC3DsDyhVK78HO657d',
                language_code = 'zh-cn',
                sessionid = 'c78nwrqpmuku2xfj8f50l0jvgjvfqx07')
完整代码:https://gist.github.com/50225512/413b2662c764dd76dc1f54f6bc900219

你可能感兴趣的:(爬虫)