这是第三方网站的登录页面吗?如果是这样的话,可能不仅仅是发布表单输入。在
例如,我只是在我自己的一个网站上尝试了登录页面。在我的例子中,一个简单的post请求不起作用,这可能与您正在访问的登录页面相同。在
对于初学者来说,登录表单可能有一个隐藏的csrf token值,您必须在发布登录请求时发送该值。这意味着您必须首先get登录页面,然后解析结果html中的csrf token值。服务器在登录请求中也可能需要其会话cookie。在
我使用requests模块来处理get/post,使用beautifulsoup来解析数据。在import requests
import zlib
from BeautifulSoup import BeautifulSoup
# first get the login page
response = requests.get('https://www.site.com')
# if content is zipped, then you'll need to unzip it
html = zlib.decompress(response.read(), 16+zlib.MAX_WBITS)
# parse the html for the csrf token
soup = BeautifulSoup(html)
csrf_token = soup.find(name='input', id='csrf_token')['value']
# now, submit the login data, including csrf token and the original cookie