gpt4 book ai didi

python - 属性错误 : 'bytes' object has no attribute 'data'

转载 作者:行者123 更新时间:2023-11-28 17:38:49 25 4
gpt4 key购买 nike

这个脚本的目的是使用python在一个网站上通过login和pass进行登录

使用登录启动脚本并作为参数传递。

[vegasus@Ph3NyX:~]$python3 grabbit.py mylogin mypass

Traceback (most recent call last):
File "/Users/vegasus/PycharmProjects/Lesson_Python/grabbit.py", line 102, in <module>
WebLogin('mylogin', 'mypass')
File "/Users/vegasus/PycharmProjects/Lesson_Python/grabbit.py", line 61, in __init__
response = self.login()
File "/Users/vegasus/PycharmProjects/Lesson_Python/grabbit.py", line 82, in login
response = self.opener.open(login_url.encode(), login_data.encode())
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/urllib/request.py", line 444, in open
req.data = data
AttributeError: 'bytes' object has no attribute 'data'

这是脚本:

import urllib, urllib.request, urllib.parse
import http.cookiejar
import sys

class WebLogin(object):

def __init__(self, username, password):

# url for website we want to log in to
self.base_url = 'https://www.mywebsite.com'
# login action we want to post data to
# could be /login or /account/login or something similar
self.login_action = '/en/login'
# file for storing cookies
self.cookie_file = 'login.cookies'

# user provided username and password
self.username = username
self.password = password

# set up a cookie jar to store cookies
self.cj = http.cookiejar.MozillaCookieJar(self.cookie_file)

# set up opener to handle cookies, redirects etc
self.opener = urllib.request.build_opener(
urllib.request.HTTPRedirectHandler(),
urllib.request.HTTPHandler(debuglevel=0),
urllib.request.HTTPSHandler(debuglevel=0),
urllib.request.HTTPCookieProcessor(self.cj)
)

# pretend we're a web browser and not a python script
self.opener.addheaders = [('User-agent',
('Mozilla/4.0 (compatible; MSIE 6.0; '
'Windows NT 5.2; .NET CLR 1.1.4322)'))
]

# open the front page of the website to set and save initial cookies
response = self.opener.open(self.base_url)
self.cj.save()

# try and log in to the site
response = self.login()

print (response.read())

# method to do login
def login(self):

# parameters for login action
# may be different for different websites
# check html source of website for specifics
login_data = urllib.parse.urlencode({
'username' : self.username,
'password' : self.password,
'remember_me' : True
})

# construct the url
login_url = self.base_url + self.login_action
# then open it
response = self.opener.open(login_url.encode(), login_data.encode())
# save the cookies and return the response
self.cj.save()
return response


if __name__ == "__main__":

args = sys.argv

# check for username and password
if len(args) != 3:
print ("Incorrect number of arguments")
print ("Argument pattern: username password")
exit(1)

username = args[1]
password = args[2]

# initialise and login to the website
WebLogin('mylogin', 'mypass')

最佳答案

问题是 self.opener.open 期望它的第一个参数是 str 类型。因此,在调用该函数时,不要对该参数调用 str.encode,只需将其作为 str 类型传递即可。

注意:这个错误的原因是因为 self.opener.open 做了一个 isinstance 检查它是否是一个字符串,如果是则它转换它们到所需的数据类型。否则,它假定它已经是所需的类型(具有字段 data)。


编辑:上面之前描述的两个参数都需要是 str 类型,这是错误的。第一个参数必须是 str 而第二个参数必须是 Nonebytes

关于python - 属性错误 : 'bytes' object has no attribute 'data' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27473432/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com