我正在尝试登录Interviewbit并使用Cookie下载受登录保护的页面。
我的代码是:
from urllib.request import build_opener, HTTPCookieProcessor
from urllib.parse import urlencode
from http.cookiejar import CookieJar
class InterviewBit:
def __init__(self):
self.interviewbit_login_url = "https://www.interviewbit.com/users/sign_in/"
self.opener = None
def login(self, username, password):
cj = CookieJar()
self.opener = build_opener(HTTPCookieProcessor(cj))
login_data = urlencode({'user_email': username, 'user_password': password, 'user_remember_me': '1'})
# Problem is in this line
binary_data = login_data.encode('UTF-8')
self.opener.open(self.interviewbit_login_url, binary_data)
print("Login successful")
def download(self, cat):
url = "https://www.interviewbit.com/search/?q%5B%5D=LinkedIn"
resp = self.opener.open(url)
client = InterviewBit()
# Enter your Interviewbit login credentials
client.login("my_email_id", "my_password")
client.download("lol")
我无法将login_data
发送为string
:
self.opener.open(self.interviewbit_login_url, login_data)
我尝试了UTF-8
和ascii
编码,但没有一个有效。我仍然收到不可处理的实体错误: urllib.error.HTTPError:HTTP错误422:无法处理的实体