微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何修复 python 请求 TypeError 错误

如何解决如何修复 python 请求 TypeError 错误

我目前正在编写一个脚本,该脚本可以登录到 study.com 并显示到期的作业。但我一直面临 TypeError 错误

这是我的代码

import requests
from bs4 import BeautifulSoup
from colorama import Fore,init

# initializing the colors
init()
init(convert=True)

# log in info
data = {
    'username': 'insert email here','password': 'insert password here','remember-me': 'on'
}

# headers
headers = {
    'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/67.0.3396.99 Safari/537.36'
}


with requests.Session() as s:
    r = s.get('https://study.com/academy/login.html')
    soup = BeautifulSoup(r.content,'html5lib')
    data['_csrf'] = soup.find('input',attrs={'name': '_csrf'})['value']
    r = s.post('https://study.com/academy/login.ajax',data=data,headers=headers)
    if 'memberDashboard' in r.text:
        print(Fore.GREEN + 'Logged in.')
    else:
        print('Error. Wrong email or password')

代码返回以下错误

Traceback (most recent call last):
  File "test2.py",line 25,in <module>
    data['_csrf'] = soup.find('input',attrs={'name': '_csrf'})['value']
TypeError: 'nonetype' object is not subscriptable

我对 python 很陌生,所以我不知道 TypeError 错误实际上意味着什么。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。