You must log in to the site through 2 forms and a button, for subsequent parsing

How to do it using the Kanna library (or any other, if you know)

Here is what I have:

func displayURL(){ let myURLAdress = "http://someshitsite.ru/mobile/shared/default.aspx" let myURL = URL(string: myURLAdress) let URLTask = URLSession.shared.dataTask(with: myURL!){ myData, response, error in guard error == nil else {return} let myHTMLString = String(data: myData!, encoding: String.Encoding.utf8) print(myHTMLString!) if let doc = HTML(html: myHTMLString!, encoding: .utf8) { print(doc.title!) for link in doc.xpath("//a | //link") { print(link.text!) } } } URLTask.resume() } 

But the forms themselves and the button

  <input class="textbox" tabindex="1" id="name" name="name" type="text" title="Логин"> <input class="textbox" tabindex="2" id="password" name="password" type="password" title="Пароль"> <input type="submit" tabindex="3" id="enter" name="enter" title="Войти" value="Войти" class="btninput"> 
  • And what does not work? What don't you like? - Bulson
  • @Bulson Yes, the fact that I can only receive the source code, but I don’t have a clue how to log in - Dmitry Memeboy

1 answer 1

Quite simply, any form is either a post or get request. You need to create a connection ... i.e. Collect a string and pass parameters to it let string = " http://site.ru?name=Name&password=Password "

Be sure to understand what kind of request you are going to do as they have different implementation methods.

here's how I login, I advise you to study web POST and GET as deeply as possible and, accordingly, swift

  собираем URL для post запроса let myUrl = URL(string: "http://site/crapi_checkauth"); var request = URLRequest(url:myUrl!) request.httpMethod = "POST" let postString = "login=\(login)&password=\(password)"; request.httpBody = postString.data(using: String.Encoding.utf8); let task = URLSession.shared.dataTask(with: request) { data, response, error in guard let data = data, error == nil else { // ошибка если нет соединения с интернетом или данные не пришли print("error=\(String(describing: error))") return } if error != nil { print("error=\(String(describing: error))") return } do { let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? NSDictionary guard let parseJSON = json else { print("Error while parsing") return } let id = parseJSON["id"] as? String // Login success if id != nil { DispatchQueue.main.async( execute: { appDelegate.login() }) } else { DispatchQueue.main.async( execute: { print("Message from =\("Ошибка тут")") }) } } catch { print(error) } } task.resume() } 

task.resume () is required to exit dispatch and make everything work

  • Strange minus slapped and did not even explain why ... strange people today - Alexander A