刚学Python,用Requests试了一下登陆V2EX并对某个主题发表评论。评论成功。但是抛出异常,评论失败时(前后两条评论内容一样时,V2ex会阻止评论),也抛出一样的异常。
想请教这个异常的原因
代码和异常如下
# coding=utf-8
import requests
from bs4 import BeautifulSoup as bs
s = requests.Session()
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36',
'Origin': 'http://www.v2ex.com',
'Referer': 'http://www.v2ex.com/signin',
'Host': 'www.v2ex.com',
}
r = s.get('http://www.v2ex.com/signin', headers=headers)
soup = bs(r.content)
once = soup.find('input', {'name': 'once'})['value']
print 'login once = ' + once
login_data = {'u': 'Registering', 'p': '1111111111', 'once': once, 'next': '/'}
s.post('https://www.v2ex.com/signin', login_data, headers=headers)
v_topic = s.get('https://www.v2ex.com/t/185169', headers=headers)
soup = bs(v_topic.content)
once = soup.find('input', {'name': 'once'})['value']
print 'title once = ' + once
comment_data = {'content': 'I am a sexy robot','once': once}
s.post('https://www.v2ex.com/t/185169', comment_data, headers=headers)
>>>
login once = 50558
title once = 59608
Traceback (most recent call last):
File "E:\python\learningCode\fakelogin.py", line 28, in <module>
s.post('https://www.v2ex.com/t/185169', comment_data, headers=headers)
File "E:\python27_9\lib\site-packages\requests\sessions.py", line 508, in post
return self.request('POST', url, data=data, json=json, **kwargs)
File "E:\python27_9\lib\site-packages\requests\sessions.py", line 465, in request
resp = self.send(prep, **send_kwargs)
File "E:\python27_9\lib\site-packages\requests\sessions.py", line 573, in send
r = adapter.send(request, **kwargs)
File "E:\python27_9\lib\site-packages\requests\adapters.py", line 415, in send
raise ConnectionError(err, request=request)
ConnectionError: ('Connection aborted.', ResponseNotReady())
>>> ================================ RESTART ================================
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.