刚学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 ================================
1
lincanbin 2015-04-27 17:00:08 +08:00 via Android
Connection aborted
估计是防火墙限制了频繁的操作,就像快速连续刷新某个页面那样? |
2
oott123 2015-04-27 20:03:09 +08:00
测试请去 /go/sandbox ~
|
3
stronger 2015-04-27 20:23:45 +08:00
怎么我用楼主的程序出现错误:
Warning (from warnings module): File "G:\Program Files\Python2.7\lib\site-packages\requests\packages\urllib3\util\ssl_.py", line 79 InsecurePlatformWarning InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning. Traceback (most recent call last): File "G:/Program Files/Python2.7/yq_py/v2ex_test.py", line 19, in <module> s.post('https://www.v2ex.com/signin', login_data, headers=headers) File "G:\Program Files\Python2.7\lib\site-packages\requests\sessions.py", line 507, in post return self.request('POST', url, data=data, json=json, **kwargs) File "G:\Program Files\Python2.7\lib\site-packages\requests\sessions.py", line 464, in request resp = self.send(prep, **send_kwargs) File "G:\Program Files\Python2.7\lib\site-packages\requests\sessions.py", line 576, in send r = adapter.send(request, **kwargs) File "G:\Program Files\Python2.7\lib\site-packages\requests\adapters.py", line 431, in send raise SSLError(e, request=request) SSLError: hostname 'www.v2ex.com' doesn't match either of '*.c3edge.com', 'c3edge.com' |
4
Registering OP |
5
stronger 2015-04-27 22:18:22 +08:00
@Registering 改成我自己的了,我的是2.7.8,还是不行呢
|
6
Registering OP @stronger 你没安装Requests和BeautifulSoup4这两个第三方库吧
|
7
stronger 2015-04-28 10:22:14 +08:00
@Registering 安装了呀,奇怪呢!!!
|
8
stronger 2015-04-28 10:23:47 +08:00
@Registering 还是之前那个错误什么SSL错误
|
9
HappyTrail 2015-04-28 16:29:47 +08:00
把楼主代码里的https都换成http之后就成功了 - -
post相同内容也没有报异常 返回里有"创建新回复过程中遇到一些问题:你上一条回复的内容和这条相同" ( |
10
stronger 2015-04-28 22:05:11 +08:00
@Registering 请问一下,post里面的once数据是什么呀?
|
11
Registering OP |