V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
Registering
V2EX  ›  Python

Python 新手提问

  •  
  •   Registering · 2015-04-27 16:16:07 +08:00 · 3851 次点击
    这是一个创建于 3492 天前的主题,其中的信息可能已经有所发展或是发生改变。

    刚学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 ================================
    
    11 条回复    2015-04-28 22:24:06 +08:00
    lincanbin
        1
    lincanbin  
       2015-04-27 17:00:08 +08:00 via Android
    Connection aborted
    估计是防火墙限制了频繁的操作,就像快速连续刷新某个页面那样?
    oott123
        2
    oott123  
       2015-04-27 20:03:09 +08:00
    测试请去 /go/sandbox ~
    stronger
        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'
    Registering
        4
    Registering  
    OP
       2015-04-27 20:59:06 +08:00
    @stronger
    代码中的账号密码有改过来?
    我用的是python2.7.9
    stronger
        5
    stronger  
       2015-04-27 22:18:22 +08:00
    @Registering 改成我自己的了,我的是2.7.8,还是不行呢
    Registering
        6
    Registering  
    OP
       2015-04-28 09:02:01 +08:00
    @stronger 你没安装Requests和BeautifulSoup4这两个第三方库吧
    stronger
        7
    stronger  
       2015-04-28 10:22:14 +08:00
    @Registering 安装了呀,奇怪呢!!!
    stronger
        8
    stronger  
       2015-04-28 10:23:47 +08:00
    @Registering 还是之前那个错误什么SSL错误
    HappyTrail
        9
    HappyTrail  
       2015-04-28 16:29:47 +08:00
    把楼主代码里的https都换成http之后就成功了 - -
    post相同内容也没有报异常 返回里有"创建新回复过程中遇到一些问题:你上一条回复的内容和这条相同" (
    stronger
        10
    stronger  
       2015-04-28 22:05:11 +08:00
    @Registering 请问一下,post里面的once数据是什么呀?
    Registering
        11
    Registering  
    OP
       2015-04-28 22:24:06 +08:00
    @stronger
    你要学会用chrome的控制台,并查看每次点击链接发送请求时的报文
    你会发现每次登陆不止发送账号密码,还有一个once字符串,该字符串是每次随机生成的,
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   907 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 20ms · UTC 22:26 · PVG 06:26 · LAX 14:26 · JFK 17:26
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.