我试图能够登录一个带有验证码的 html 表单的网站。我一直在尝试这样做的方式是获取登录表单的 html,向用户显示验证码,用户将其放入文本字段,然后我尝试提交表单。
我总是得到的错误是无效的密钥代码,所以我猜问题是我在第一个实例中获取的验证码,对第二个无效......有什么想法可以做到这一点吗?
该网页是同人小说,我作为个人项目这样做,看看我是否能够导出我的收藏夹列表并关注。
我这样做是为了向用户显示验证码。
NSURL *url = [NSURL URLWithString:@"https://www.fanfiction.net"];
self.httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
[self.httpClient getPath:@"/login.php" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
TFHpple * doc = [[TFHpple alloc] initWithHTMLData:responseObject];
NSArray * elements = [doc searchWithXPathQuery:@"//img[@id='xcaptcha']"];
TFHppleElement * element = [elements objectAtIndex:0];
[self.captchaView setImageWithURL:[NSURL URLWithString:[element objectForKey:@"src"]]];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) { }];
然后,当用户在文本字段中输入验证码并按下 UIButton 时,我会这样做
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
kFFNMail, @"email",
kFFNPass, @"password",
self.captchaField.text, @"captcha",
nil];
NSURLRequest *postRequest = [self.httpClient multipartFormRequestWithMethod:@"POST"
path:@"/login.php"
parameters:params
constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { }];
/*
// I think this is the same as the one before in this case
NSMutableURLRequest *postRequest = [self.httpClient requestWithMethod:@"POST"
path:@"/login.php"
parameters:params2];
*/
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:postRequest];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
TFHpple * doc = [[TFHpple alloc] initWithHTMLData:responseObject];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) { }];
[operation start];
如果有任何迹象,我可以在这样的 ruby 脚本中使用它
require 'rubygems'
require 'mechanize'
require "highline/import"
a = Mechanize.new
a.get('https://www.fanfiction.net/login.php') do |page|
images = page.search("#xcaptcha")
a.get(images.first.attributes["src"]).save "captcha.jpg"
# I read the saved image,and enter the captcha code
captcha = ask "Input captcha: "
# Submit the login form
my_page = page.form_with(:action => '/login.php') do |f|
f.email = my_mail
f.password = my_pass
f.captcha = captcha
end.click_button
# already logged!
a.get('https://www.fanfiction.net/alert/story.php') do |page|
page.links.each do |link|
text = link.text.strip
next unless text.length > 0
puts text
end
end
end