猿问

Python Urllib Urlopen在换行后不会返回吗?

我正在尝试通过尝试访问的API进行身份验证。我正在使用urllib.parse.urlencode对URL中的参数进行编码。我正在使用urllib.request.urlopen来获取内容。


这应该从服务器返回3个值,例如:


SID=AAAAAAAAAAA

LSID=BBBBBBBBBBB

AUTH=CCCCCCCCCCC

问题在于它仅返回第一个值和尾随的换行符。


import urllib.request

import urllib.parse


Emailparamx = 'Email'

Emailparam = Emailparamx.encode('utf-8')

email = 'myemail@stackoverflow.com'

email = email.encode('utf-8')

Passwdparam = 'Passwd'

Passwdparam = Passwdparam.encode('utf-8')

password = 'hidden'

password = password.encode('utf-8')

Accounttypeparam = 'accountType'

Accounttypeparam = Accounttypeparam.encode('utf-8')

accounttype = 'GOOGLE'

accounttype = accounttype.encode('utf-8')

Serviceparam = 'service'

Serviceparam = Serviceparam.encode('utf-8')

service = 'adwords'

service = service.encode('utf-8')



url = 'https://accounts.google.com/ClientLogin?'

urlen = url.encode('utf-8')

data = [(Emailparamx, email), (Passwdparam, password),

        (Accounttypeparam, accounttype), (Serviceparam, service)]




auth = ''


dataurl = urllib.parse.urlencode(data)



accessurl = (url + "%s" % dataurl)


fh = urllib.request.urlopen(accessurl)


equals = '='

eqenc = equals.encode('utf-8')


try:

  msg = fh.readline().split(eqenc)

  print (msg)

然后味精打印


[b'SID', b'AAAAAAAAAAAAAAAAA\n']

我知道那是一些非常丑陋的代码,我使用Python大约一个星期了。任何帮助将不胜感激。


杨魅力
浏览 192回答 1
1回答
随时随地看视频慕课网APP

相关分类

Python
我要回答