Python 新手提问

2015-04-27 16:16:07 +08:00
 Registering

刚学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 ================================
3889 次点击
所在节点    Python
11 条回复
lincanbin
2015-04-27 17:00:08 +08:00
Connection aborted
估计是防火墙限制了频繁的操作,就像快速连续刷新某个页面那样?
oott123
2015-04-27 20:03:09 +08:00
测试请去 /go/sandbox ~
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'
Registering
2015-04-27 20:59:06 +08:00
@stronger
代码中的账号密码有改过来?
我用的是python2.7.9
stronger
2015-04-27 22:18:22 +08:00
@Registering 改成我自己的了,我的是2.7.8,还是不行呢
Registering
2015-04-28 09:02:01 +08:00
@stronger 你没安装Requests和BeautifulSoup4这两个第三方库吧
stronger
2015-04-28 10:22:14 +08:00
@Registering 安装了呀,奇怪呢!!!
stronger
2015-04-28 10:23:47 +08:00
@Registering 还是之前那个错误什么SSL错误
HappyTrail
2015-04-28 16:29:47 +08:00
把楼主代码里的https都换成http之后就成功了 - -
post相同内容也没有报异常 返回里有"创建新回复过程中遇到一些问题:你上一条回复的内容和这条相同" (
stronger
2015-04-28 22:05:11 +08:00
@Registering 请问一下,post里面的once数据是什么呀?
Registering
2015-04-28 22:24:06 +08:00
@stronger
你要学会用chrome的控制台,并查看每次点击链接发送请求时的报文
你会发现每次登陆不止发送账号密码,还有一个once字符串,该字符串是每次随机生成的,

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/186716

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX