How to get dynamic loaded HTML in Swift - html

I can use Alamofire to return the HTML of a page, but some div's content is empty.
If I open chrome's developer console I can see them in ELements tab rather than Source tab.
How can I get those contents in Swift?

Look this method :
+ stringWithContentsOfURL:encoding:error (docs)
//the content url
let urlString = "https://google.com"
guard let theURL = URL(string: urlString) else {
print("Error: \(urlString) doesn't seem to be a valid URL")
return
}
do {
let myHTMLString = try String(contentsOf: theURL, encoding: .ascii)
print("html : \(myHTMLString)")
} catch let error {
print("Error: \(error)")
}

Related

Bundle.main.path(forResource:ofType:inDirectory:)

I am trying to load a json file from my bundle, but only say the first 100 entries. Is it possible to use Bundle.main.path(for...) with something like the "&limit=Int" used on the url for fetching from an API?
I want to NOT have to load the entire file into my array.
I'm using a simple/nice example from Paul Hudson json fetching songs from itunes api. This loader works fine. I'm just wondering how to handle a larger file, for later on. I have found the .prefix(Int) to trim my array, but I'd like to not have to load the entire file.
'''
func loadData() {
/* guard let url = URL(string: "https://itunes.apple.com/search?term=taylor+swift&entity=song&limit=\(count)") else {
print("Invalid URL")
return
} */
guard let url = Bundle.main.url(forResource: "TaylorSwift12", withExtension: "json") else { fatalError("Could not find TaylorSwift.json") }
let request = URLRequest(url: url)
URLSession.shared.dataTask(with: request) { data, response, error in
if let data = data {
if let decodedResponse = try? JSONDecoder().decode(Response.self, from: data) {
DispatchQueue.main.async {
self.results = decodedResponse.results
}
return
}
}
print("Fetch failed: \(error?.localizedDescription ?? "Unknown error")")
}.resume()
}
'''

How to set the timeout in swift trying to catch html code?

I'm trying too get the html code from a specific URL with this code:
guard let myURL = URL(string: myURLString) else {
print("Error: \(myURLString) doesn't seem to be a valid URL")
return myLinksArray
}
do {
myHTMLString = try String(contentsOf: myURL, encoding: .ascii)
print("HTML : \(myHTMLString)")
} catch let error {
print("Error: \(error)")
}
but in this way I'm unable to set a timeout. Some websites require too much time to get response.
What's the best way to solve the problem?
Thanks in advance
Have you considered creating an URLSession dataTask, I believe it would be a better approach. You can create a custom URLSessionConfiguration and use it to specify the timeoutIntervalForRequest or timeoutIntervalForResource according to your needs like this:
guard let myURL = URL(string: myURLString) else {
print("Error: \(myURLString) doesn't seem to be a valid URL")
fatalError()
}
let sessionConfig = URLSessionConfiguration.default
sessionConfig.timeoutIntervalForRequest = 15 // seconds
sessionConfig.timeoutIntervalForResource = 15 // seconds
let dataTask = URLSession(configuration: sessionConfig).dataTask(with: myURL) { (data, urlResponse, error) in
guard let data = data else { return }
let myHTMLString = String(data: data, encoding: .ascii)
print("HTML : \(myHTMLString ?? "")")
}
dataTask.resume() // start the request

Swift JSON Parsing with a filter in url

I am trying to parse JSON, everything works fine, but when I try to give a filter with the url, it says found nil while unwrapping an optional value. I was wondering if I should give the filters in some other way. PS.. The url works fine when I use it in the browser
this is how the url with filter looks like:
https://start.jamespro.nl/v4/api/json/tasks/?filter=[{"column":"Date","operator":"=","value":"2017-08-04"}, {"column":"UserId","operator":"=","value":"2"}]
and this is my whole code:
func apiRequestTasks(url: String) {
apiRequestHeader(userName: "*******", passWord: "******")
var running = false
let urlProjects = NSURL(string: url)
let task = session?.dataTask(with: urlProjects! as URL) {
( data, response, error) in
if let taskHeader = response as? HTTPURLResponse {
print(taskHeader.statusCode)
}
if error != nil {
print("There is an error!!!")
print(error ?? "")
} else {
if let content = data {
do {
let dictionary = try JSONSerialization.jsonObject(with: content) as! [String:Any]
print(dictionary)
}
catch {
print("Error: Could not get any data")
}
}
}
running = false
}
running = true
task?.resume()
while running {
print("waiting...")
sleep(1)
}
}
I think the problem is the way you create your URL, try something like this:
let filters = "[\"column\":\"Date\",\"operator\":\"=\",\"value\":\"2017-08-04\"}, {\"column\":\"UserId\",\"operator\":\"=\",\"value\":\"2\"}]"
if var url = URLComponents(string: "https://start.jamespro.nl/v4/api/json/tasks") {
url.query = "filter=:\(filters)"
print ("url", url.string ? "invalid url")
}
I just encoded the filter part of my url and it worked. But thanx for the reactions !!!
let filter = "[{\"column\":\"Date\",\"operator\":\"=\",\"value\":\"2017-08-04\"}, {\"column\":\"UserId\",\"operator\":\"=\",\"value\":\"2\"}]"
let encodedFilter = filter.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed)
let baseUrl = "https://start.jamespro.nl/v4/api/json/tasks/?filter="
let url = baseUrl + encodedFilter!
apiRequestTasks(url: url)

Swift get content of html from url

let myURLString = "https://en.wiktionary.org/wiki/see"
if let myURL = NSURL(string: myURLString) {
let myHTMLString = String(contentsOfURL: myURL, encoding: String.Encoding.utf8)
print("HTML : \(myHTMLString)")
}
And I got printed:
HTML : (https://en.wiktionary.org/wiki/see, Unicode (UTF-8))
But instead I need html content.
What I am doing wrong?
Update:
As a source for the code I used: How To Get HTML source from URL with Swift
Please, read my question with more attention, as the result I got text of link, but instead I need text of html page
Try this:
let myURLString = "http://google.com"
guard let myURL = NSURL(string: myURLString) else {
print("Error: \(myURLString) doesn't seem to be a valid URL")
return
}
do {
let myHTMLString = try String(contentsOfURL: myURL)
print("HTML : \(myHTMLString)")
} catch let error as NSError {
print("Error: \(error)")
}
Hope this helps!
To retrieve the HTML of the webpage referenced by a url you just need to
let myURLString = "https://en.wiktionary.org/wiki/see"
if let
url = NSURL(string: myURLString),
html = try? String(contentsOfURL: url) {
print(html)
}
I tested this code in my Playground and it is retrieving the full HTML of the web page.
Solution: instead of String, use NSString
let myURLString = "https://en.wiktionary.org/wiki/see"
if let myURL = NSURL(string: myURLString) {
do {
let myHTMLString = try NSString(contentsOf: myURL as URL, encoding: String.Encoding.utf8.rawValue)
print("html \(myHTMLString)")
} catch {
print(error)
}
}

Parsing JSON data (from URL) in Swift

I'm trying to retrieve some data from an URL thanks to JSON. Here's my swift code:
// get symbol asked
let symbol = symbolField.text!
// define URL
let url = NSURL(string: "http://yahoojson.gobu.fr/symbol.php?symbol=\(symbol)")!
let task = NSURLSession.sharedSession().dataTaskWithURL(url) { (data, response, error) -> Void in
if let urlContent = data {
do {
let jsonResult = try NSJSONSerialization.JSONObjectWithData(urlContent, options: NSJSONReadingOptions.MutableContainers)
print(jsonResult)
} catch {
print("Error JSON")
}
}
}
task.resume()
Everything seems to work fine, but the "do-try-catch" always prints "Error JSON". My code seems unable to convert my URL content into actual JSON. Any idea what I am doing wrong?
The URL returns html/javascript not pure json.
Paste the URL into your browser and look at the source code.
A side note: replace
print("Error JSON")
with
print(error)
to get more specific error information