我一直在研究一个程序,该程序可以让您登录不同的网页,并告诉您登录是否成功。它的工作原理如下:1.登录到您已知存在且正确的帐户,然后将响应文本复制到文件中; 2.登录到您知道不起作用的帐户,然后执行相同的操作。然后,当您尝试登录到该网页时,程序将比较响应与先前保存的响应并找到相似之处。如果发现与正确的登录文本有更多相似性,则意味着成功了,否则就成功了。但这似乎不起作用,我也不知道为什么。我认为这是因为用于比较响应的机制,但我不确定。我将代码留在这里。感谢您的回答!
import requests
def checkStatus(response): #we check if the response let us login by comparing it to previous responses. This algorythm should be able to work for every webpage
check1 = 0
check2 = 0
file1 = open("wronglogin.txt", "r")
file2 = open("correctlogin.txt", "r")
i = 0
for line in response:
try:
if file2.read(i) == line:
check2 += 1
i+=1
except:
break
i = 0
for line in response:
try:
if file1.read(i) == line:
check1 += 1
i+=1
except:
break
print(check1)
print(check2)
if check1 < check2:
return True
else:
return False
url = "https://twitter.com/explore"
username = "your_username"
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36'}
password = "your_password"
form_data = { "username":str(username), "password":str(password) }
s = requests.Session()
response = s.post(url, headers = headers, data = form_data)
print(response.text)
if checkStatus(response.text) == True:
print(form_data["password"])
答案 0 :(得分:0)
再检查一下代码后,我发现它不是在比较我逐行发布时得到的响应的内容,而是逐位地显示,而是以行的形式读取文件的内容。当然,这意味着它将无法工作,因此,我建议您先保存要检查的响应内容到文件中,以便逐行读取。但是,也就是您要搜索的信息。无论您是否正确登录的信息,都可能不在内容中,而是在标头,Cookie等中。要检查所有这些因素,我发现了一个小技巧。就像使用数组一样,您实际上可以创建一个for循环来检查它可能具有的所有变量。看起来像这样:
import requests
url = "https://twitter.com/explore"
username = "your_username"
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36'}
password = "your_password"
form_data = { "username":str(username), "password":str(password) }
Session = requests.session()
response = Session.post(url, data=data, headers=headers)
for var in response:
print(var)
var将是存储在响应中的数据。我不确定100%是否是所有数据://