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.

6
  • check for the content which is available only for the logged-in users...? Commented Jan 18, 2019 at 8:13
  • I need contents of when the user is not logged in. Commented Jan 18, 2019 at 8: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? Commented Jan 18, 2019 at 8: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. Commented Jan 18, 2019 at 8: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. Commented Jan 18, 2019 at 8:25

1 Answer 1

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()
Sign up to request clarification or add additional context in comments.

1 Comment

This works for me. I also added request.HTTPShouldHandleCookies = false

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.