Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python请求转换为urlib2_Python_Python Requests_Urllib2 - Fatal编程技术网

Python请求转换为urlib2

Python请求转换为urlib2,python,python-requests,urllib2,Python,Python Requests,Urllib2,我可以使用requestspackage登录并正确获取源页面。由于某种原因,我只能使用标准库。不幸的是,urlib2没有得到与请求相同的结果,我错过了什么吗 请求 urllib2 因为您没有cookie,webserver将写入/检查,而request将自动执行操作。 将urlib2与cookielib一起使用,如下所示: import urllib2 import cookielib def login(userName, passWord): url = "https://login

我可以使用
requests
package登录并正确获取源页面。由于某种原因,我只能使用标准库。不幸的是,
urlib2
没有得到与
请求
相同的结果,我错过了什么吗

请求 urllib2
因为您没有
cookie
webserver
将写入/检查,而
request
将自动执行操作。
urlib2
cookielib
一起使用,如下所示:

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')

顺便说一句,您现在需要更改密码

是否有任何错误消息?
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
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')