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
coordinate
V2EX  ›  Python

如何使用 Python 登陆 v2ex?

  •  
  •   coordinate · 2017-07-30 16:26:33 +08:00 · 3108 次点击
    这是一个创建于 2468 天前的主题,其中的信息可能已经有所发展或是发生改变。

    初学 python,想尝试用 python 登陆 v2ex。 第一次尝试,使用登录信息登陆

    import requests
    from bs4 import BeautifulSoup
    
    url = r"https://www.v2ex.com/signin"
    v2ex_session = requests.Session()
    
    f = v2ex_session.get(url)
    soup = BeautifulSoup(f.content,"html.parser")
    once = soup.find('input',{'name':'once'})['value']
    
    user = {'u':'username', 'p':'password', 'once': once, 'next': '/'}#这里的 p 和 u 在浏览器上看是乱码,我想问一下,这个乱码是什么编码,会一直改变吗?
    
    v2ex_session.post(url, data = user)
    f = v2ex_session.get('http://www.v2ex.com/settings')
    print f.content
    

    第二次尝试,使用 cookie,不贴代码了,最终也是失败了,是因为 https 的原因吗?可否使用 cookies 登陆 https 的链接呢?

    最后希望告知登陆的正确姿势,万分感谢!!!

    10 条回复    2017-07-31 09:10:48 +08:00
    RLib
        1
    RLib  
       2017-07-30 16:52:38 +08:00
    那个乱码是随机生成的表单名,在登录前先获取
    linw1995
        2
    linw1995  
       2017-07-30 17:16:42 +08:00
    (⊙﹏⊙)跟一楼一样发现了这个问题,换了表单名,可是还是不行。
    giuem
        3
    giuem  
       2017-07-30 17:20:29 +08:00   ❤️ 1
    ranleng
        4
    ranleng  
       2017-07-30 18:29:15 +08:00
    加 Header ?
    tumbzzc
        5
    tumbzzc  
       2017-07-30 18:49:57 +08:00 via Android
    提交表单的 user 和 password 的字段名是随机的
    madfishy
        6
    madfishy  
       2017-07-30 19:47:08 +08:00
    想采集,哈哈
    deeeeeeep
        7
    deeeeeeep  
       2017-07-30 19:48:56 +08:00
    with requests.Session() as s:
    req = s.get(url)
    print("cookies="+repr(req.cookies))
    html = req.text
    user = re.findall(r'input\s+type="text"\s+class="sl"\s+name="(\S+)"',html)
    key = re.findall(r'input\s+type="password"\s+class="sl"\s+name="(\S+)"',html)
    once = re.findall(r'input\s+type="hidden"\s+value="(\S+)"\s+name="once"',html)
    postdata = {user[0]:"uuuuuu",key[0]:"123456","once":once[0],"next":"/"}
    print("data="+repr(postdata))

    login_req = requests.post(url,headers=headers,data=postdata,cookies=req.cookies)
    coordinate
        8
    coordinate  
    OP
       2017-07-30 20:08:49 +08:00
    终于 get 到了问题的关键,这里要谢谢 @giuem
    在 header 后面要加这个
    session.headers.update({'referer': url}) #referer 是关键
    numberwolf
        9
    numberwolf  
       2017-07-30 22:37:25 +08:00
    你们这样真的好吗...
    lieh222
        10
    lieh222  
       2017-07-31 09:10:48 +08:00
    能不能偷摸着做,没准因为你一个帖子大家都得改
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2120 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 10:56 · PVG 18:56 · LAX 03:56 · JFK 06:56
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.