如何使用 Python 登陆 v2ex? - V2EX
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 年 7 月 30 日 3701 次点击
    这是一个创建于 3086 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

    import requests from bs4 import BeautifulSoup url = r"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 年 7 月 30 日
    那个乱码是随机生成的表单名,在登录前先获取
    linw1995
        2
    linw1995  
       2017 年 7 月 30 日
    (⊙⊙)跟一楼一样发现了这个问题,换了表单名,可是还是不行。
    giuem
        3
    giuem  
       2017 年 7 月 30 日   1
    ranleng
        4
    ranleng  
       2017 年 7 月 30 日
    加 Header ?
    TKKONE
        5
    TKKONE  
    PRO
       2017 年 7 月 30 日 via Android
    提交表单的 user 和 password 的字段名是随机的
    madfishy
        6
    madfishy  
       2017 年 7 月 30 日
    想采集,哈哈
    deeeeeeep
        7
    deeeeeeep  
       2017 年 7 月 30 日
    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 年 7 月 30 日
    终于 get 到了问题的关键,这里要谢谢 @giuem
    在 header 后面要加这个
    session.headers.update({'referer': url}) #referer 是关键
    numberwolf
        9
    numberwolf  
       2017 年 7 月 30 日
    你们这样真的好吗...
    lieh222
        10
    lieh222  
       2017 年 7 月 31 日
    能不能偷摸着做,没准因为你一个帖子大家都得改
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     920 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 30ms UTC 20:02 PVG 04:02 LAX 12:02 JFK 15:02
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86