python模拟登入oschina 出现403

首席执行官机器人 发布于 2017/09/26 14:52
阅读 176
收藏 0

闲来无事,想用python模拟登入试试,就模拟了oschina登入,结果我确实湿了一下,不行啊.

#!/usr/bin/env python
# coding=utf-8
import sys;
import urllib2
from cookielib import LWPCookieJar
import httplib
reload(sys);
sys.setdefaultencoding('UTF8');
cookiejar = LWPCookieJar();
cookiesuppor = urllib2.HTTPCookieProcessor(cookiejar);
opener = urllib2.build_opener(cookiesuppor, urllib2.HTTPHandler);
urllib2.install_opener(opener);
login='https://www.oschina.net/home/login?goto_page=https%3A%2F%2Fwww.oschina.net%2F'
oschina='https://www.oschina.net'
data = {};
data['email'] = '981880785@qq.com';
data['pwd']='自己的密码';
data['save_login']='1';
data['verifyCode']='';
print(str(data));
try:
    #获取cookie
    request = urllib2.Request(url=oschina);
    urllib2.urlopen(request)
    urllib2.urlopen(urllib2.Request(url='https://hm.baidu.com/hm.gif?cc=0&ck=1&cl=24-bit&ds=2400x1350&ep=2846%2C2845&et=3&ja=0&ln=en-us&lo=0&lt=1504588289&rnd=1648206150&si=a411c4d1664dd70048ee98afe7b28f0b&su=https%3A%2F%2Fwww.oschina.net%2F%3Fnocache%3D1506407808861&v=1.2.24&lv=3&sn=20712'))
    urllib2.urlopen(urllib2.Request(url='https://www.oschina.net/action/user/captcha'))

    request = urllib2.Request(url=login, data=data);
    #request = urllib2.Request(url=login);
    
    request.add_header("Content-Type", "application/x-www-form-urlencoded; charset=utf-8")
    request.add_header("X-Requested-Width", 'XMLHttpRequest')

    request.add_header('User-Agent',
                       'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36')

    request.add_header('Referer', 'https://www.oschina.net/home/login?goto_page=https://www.oschina.net')
    request.add_header('Host', 'www.oschina.net')
    request.add_header('Accept', '*/*')
    # request.add_header('Accept-Encoding', 'gzip, deflate')
    result = urllib2.urlopen(request).read()
    print(result);
except httplib.error as e:
    print(e);
except urllib2.URLError as e:
    print(e);

 

总是 HTTP Error 403: Forbidden

加载中
返回顶部
顶部