Python 程序貌似没问题 ,登录豆瓣为啥失败呢? - 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
istill
V2EX    Python

Python 程序貌似没问题 ,登录豆瓣为啥失败呢?

  •  
  •   istill 2018-07-31 10:41:47 +08:00 1908 次点击
    这是一个创建于 2712 天前的主题,其中的信息可能已经有所发展或是发生改变。
    程序运行后,打印页面,页面仍然是未登录状态。但没发现哪里有问题啊,求高手指点。(由于隐私,这里的用户名和密码是随便写的,但测试的时候用的真实有效的账户密码)
    # -*- coding: utf-8 -*-
    import requests
    from bs4 import BeautifulSoup
    from PIL import Image
    from io import BytesIO

    url1 = "https://accounts.douban.com/login"
    url2 = "https://www.douban.com/contacts/list"
    username = "123456789@@@.com"
    passsword = "123456789"

    header = {
    "Referer":"https://www.douban.com/login",
    "User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36",
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8"
    }

    respOnse= requests.get(url1)
    response2 = response.text #获取网页登录页面的 HTML 代码
    # print(response2)

    def captcha(): #查找登录页面是否需要验证码
    if "captcha_image" in response2:
    soup1 = BeautifulSoup(response2, 'html.parser')
    soup2 = soup1.find_all(attrs={"name": "captcha-id"})
    soup2 = str(soup2)
    soup3 = BeautifulSoup(soup2, 'html.parser') #获取验证码 ID
    global captchaid
    captchaid = soup3.input["value"]
    # print(captchaid)
    yzm_url1 = soup1.find_all(attrs={"id": "captcha_image"})
    yzm_url1 = str(yzm_url1)
    yzm_url2 = BeautifulSoup(yzm_url1,'html.parser')
    yzm_url3 = yzm_url2.img["src"] #获取验证码的 URL
    yzm_respOnse= requests.get(yzm_url3)
    image = Image.open(BytesIO(yzm_response.content))
    image.show()
    yzm_shuru = input("输入验证码:")
    global captcha_solution
    captcha_solution = yzm_shuru
    global postData_has_captcha
    postData_has_captcha = { # 有验证码的 post 数据
    "source": "None",
    "redir": "https://www.douban.com",
    "form_email": username,
    "form_password": passsword,
    "captcha-solution": captcha_solution,
    "captcha-id": captchaid,
    "login": "登录"
    }
    return "有验证码"
    else:
    return "无验证码"

    panduan_yzm = captcha()

    postData_no_captcha ={ #无验证码的 post 数据
    "source":"None",
    "redir":"https://www.douban.com",
    "form_email":username,
    "form_password":passsword,
    "login":"登录"
    }

    def denglu_has_captcha(): #有验证码的登录函数
    global s
    s = requests.session()
    f = s.post(url1,header,postData_has_captcha)

    def denglu_no_captcha(): #无验证码的登录函数
    global s
    s = requests.session()
    f = s.post(url1,header,postData_no_captcha)

    if panduan_yzm == "有验证码": #开始登陆
    denglu_has_captcha()
    print("有验证码,已登录")
    r = s.get(url2)
    print(r.text)
    elif panduan_yzm == "无验证码":
    denglu_no_captcha()
    print("无验证码,已登录")
    r = s.get(url2)
    print(r.text)
    else:
    pass
    目前尚无回复
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2428 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 27ms UTC 11:46 PVG 19:46 LAX 03:46 JFK 06:46
    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