0

I am trying to scrape html code from a URL using Swift, but it needs to be done anonymously. The issue with my case is that the content differs when I am logged in the website or not. My app has a webkit that allows users to view a certain page of a website. If they log in via the webkit, the scraping result is different from if they did not log in.

I have read and searched through tons of questions on StackOverflow to see how to get the HTML source from a URL and the one that I am using can be found here: How To Get HTML source from URL with Swift I have also tried the codes from different answers and questions.

guard let getHTMLString = try? String(contentsOf: theUrl, encoding: .utf8).condensingWhitespace() as String else {
        callErrorPage()
        return
    }

I get the content of the html page with the code above.

But like I mentioned above, the codes differ if the user has logged in the website.

Edit: I need the code to always be the version where the user is not logged in.

Mr.Kushwaha
  • 491
  • 5
  • 14
Seifer
  • 11
  • 4
  • check for the content which is available only for the logged-in users...? – holex Jan 18 '19 at 08:13
  • I need contents of when the user is not logged in. – Seifer Jan 18 '19 at 08:14
  • In many cases the login information is stored in a cookie. If this is the case use `URLSession` and `URLRequest` which is able to ignore cookies. (`httpShouldHandleCookies = false`). The API `String(contentsOf` for a remote URL is inappropriate anyway. And why do you cast a `String` to a `String`? – vadian Jan 18 '19 at 08:15
  • you know, your question does not make any sense at all... if _you_ don't know what you are looking for, how does anyone else would know...? your question in its current state cannot be answered at all – speculations can be made only. – holex Jan 18 '19 at 08:16
  • @holex I know what I want. I want to get the html code of a website. But the problem is the code is different depending on if I am logged in to the website. I want the code of a non-logged in user. – Seifer Jan 18 '19 at 08:25
  • @vadian, I will try your suggestion out using urlsession and request. I removed the casting, not sure why I put it there. – Seifer Jan 18 '19 at 08:26

1 Answers1

3

It will always do this anonymously :

var request = URLRequest(url: URL(string: "http://google.com")!)
request.httpMethod = "GET"
let session = URLSession.init(configuration: URLSessionConfiguration.default)
session.dataTask(with: request) {data,response,error in
    if let data = data {
       let contents = String(data: data, encoding: .ascii)
    }
}.resume()
Emre
  • 186
  • 6