下面是一些代码,我一直试图使用这些代码登录到cook'sIllustrated网站(https://www.cooksillustrated.com/sign_in)。在
我启动一个会话,获取身份验证令牌和一个隐藏的编码字段,然后传递email和password字段的“name”和“value”(通过检查chrome中的元素找到)。表单似乎不包含任何其他元素;但是,post方法不让我登录。在
我注意到所有CSRF标记都以“==”结尾,所以我尝试删除它们。但没用。在
我还尝试修改post以使用表单输入的“id”字段而不是“name”(只是一个模糊的例子,真的…name似乎应该可以从我在其他示例中看到的情况下使用)。在
任何想法都将不胜感激。在import requests, lxml.html
s = requests.session()
# go to the login page and get its text
login = s.get('https://www.cooksillustrated.com/sign_in')
login_html = lxml.html.fromstring(login.text)
# find the hidden fields names and values; store in a dictionary
hidden_inputs = login_html.xpath(r'//form//input[@type="hidden"]')
form = {x.attrib['name']: x.attrib['value'] for x in hidden_inputs}
print(form)
# I noticed that they all ended in two = signs, so I tried taking that off
# form['authenticity_token'] = form['authenticity_token'][:-2]
# this adds to the form payload the two named fields for user name and password
# found using the "inspect elements" on the login screen
form['user[email]'] = 'my_email'
form['user[password]'] = 'my_pw'
# this uses "id" instead of "name" from the input fields
#form['user_email'] = 'my_email'
#form['user_password'] = 'my_pw'
response = s.post('https://www.cooksillustrated.com/sign_in', data=form)
print(form)
# trying to see if it worked - but the response URL is login again instead of main page
# and it can't find my name
# responses are okay, but I think that just means it posted the form
print(response.url)
print('Christopher' in response.text)
print(response.status_code)
print(response.ok)