【发布时间】:2016-11-29 19:12:10
【问题描述】:
我正在使用 python 中的请求模块,但遇到了一个问题。
我使用 Session 类请求登录网站 (http://coinplants.com)。登录后,我试图读取页面的 html,我意识到响应对象仅显示 html 正文及其内容,但不显示 html 头部。我想获得带有元标记的 html 头。知道我做错了什么吗?
s = requests.Session()
r = s.post('http://coinplants.com', data=postData)
print r.text
提前致谢:)
登录
为了废弃我使用 BeautifulSoup 的真实性令牌
soup = BeautifulSoup(r.text, 'lxml')
finding = soup.find('input', {'name' : 'authenticity_token'})
postData = {'utf8' : '%E2%9C%93', 'authenticity_token' : '',
'account[email]' : self.username, 'account[password]' : self.password,
'account[remember_me]' : '0', 'commit' : 'Log+in'}
postData['authenticity_token'] = finding['value']
r = s.post('http://coinplants.com/accounts/sign_in', data=postData)
解决方案
好的,我找到了解决问题的方法。我不知道为什么会话没有给我整个 html 内容。我从会话对象中取出 cookie 并将其添加到请求对象中:
cookies = {'_faucet:session' : s.cookies['_faucet_session']}
r = requests.get('http://coinplants.com', cookies=cookies)
print r.text
s 是会话对象。当我打印响应对象的文本时,它会显示整个 html 内容,包括 head 标签。如果有人知道为什么会话对象没有显示它,请告诉我:)
【问题讨论】:
-
您确定登录成功了吗?
-
是的,它是成功的。我登录后检查了html。
-
您可以添加帖子数据,显然减去您的电子邮件/密码吗?我看到需要一个 csrf 令牌,所以除非你正在抓取,否则我看不到你的登录是如何成功的
-
我在问题中添加了登录信息。
-
看起来不错,你确定在浏览器中选择查看源时看到了head标签吗?
标签: python html python-requests