Python请求转换为urlib2

时间:2017-06-16 03:16:17

标签: python python-requests urllib2

我可以使用requests包登录并正确获取源页面。由于某种原因,我只能使用标准库。遗憾的是urlib2request没有得到相同的结果,我错过了一些东西吗?

请求

def login(userName, passWord):
        url = "https://logindict.youdao.com/login/acc/login"
        payload = "username=" + userName + "&password=" + passWord + \
            "&savelogin=1&app=web&tp=urstoken&cf=7&fr=1&ru=http%3A%2F%2Fdict.youdao.com%2Fwordbook%2Fwordlist%3Fkeyfrom%3Dnull&product=DICT&type=1&um=true"
        headers = {
            'cache-control': "no-cache",
            'content-type': "application/x-www-form-urlencoded"
        }
        s = requests.session()
        response = s.post(url, data=payload, headers=headers)
        print response.text

的urllib2

url = "https://logindict.youdao.com/login/acc/login"
data = 'app=web&tp=urstoken&cf=3&fr=1&ru=http%3A%2F%2Fdict.youdao.com%2Fwordbook%2Fwordlist%3Fkeyfrom%3Dlogin_from_dict2.index&product=DICT&type=1&um=true&username=xxx&password=xxx'
headers = {
    'cache-control': "no-cache",
    "Content-Type": "application/x-www-form-urlencoded"
}

req = urllib2.Request(url, data=value, headers=headers)
response = urllib2.urlopen(req)
the_page = response.read()
print the_page

1 个答案:

答案 0 :(得分:1)

因为cookie webserver不会写/检查,request会自动执行。{ 将urllib2cookielib一起使用,如下所示:

import urllib2
import cookielib
def login(userName, passWord):
    url = "https://logindict.youdao.com/login/acc/login"
    payload = "username=" + userName + "&password=" + passWord + \
        "&savelogin=1&app=web&tp=urstoken&cf=7&fr=1&ru=http%3A%2F%2Fdict.youdao.com%2Fwordbook%2Fwordlist%3Fkeyfrom%3Dnull&product=DICT&type=1&um=true"
    headers = {
        'cache-control': "no-cache",
        'content-type': "application/x-www-form-urlencoded"
    }
    url = url + '?' + payload
    req = urllib2.Request(url, headers=headers)
    cookie = cookielib.CookieJar()
    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie))
    urllib2.install_opener(opener)
    response = urllib2.urlopen(req)
    the_page = response.read()
    print the_page

login('xxxxxx','xxxxx')

BTW,你现在需要改变你的密码!