I want to code an app which shows you the data of a specific HTML-Page.
How can read the text e.g. in a div and turn that text into a String? How can I search something on that page or look in the row of a table?
You need mind what is the step you need to complete this task.
First need load html page data
After find what do you need with parser.
In the first step I found this code
let url = NSURL(string: "http://www.stackoverflow.com")
let task = NSURLSession.sharedSession().dataTaskWithURL(url!) {(data, response, error) in
println(NSString(data: data, encoding: NSUTF8StringEncoding))
}
task.resume()
Example from this link How to make an HTTP request in Swift?
Look this example, you do a request and return a data, this data need be String. After received data you need parse to findo what do you want.
Second step you need tools to execute, the simple way is do with string methods like slipt. If you need something more smart I recommend Regular Expression
Simple example for split (swift 1.2)
var fullName = "First Last"
var fullNameArr = split(fullName) {$0 == " "}
var firstName: String = fullNameArr[0]
var lastName: String? = fullNameArr.count > 1 ? fullNameArr[1] : nil
example (swift 2.0)
let fullName = "First Last"
let fullNameArr = split(fullName.characters){$0 == " "}.map(String.init)
fullNameArr[0] // First
fullNameArr[1] // Last
Examples from Swift: Split a String into an array
This link is good about Regular Expression, mind it is a powerfull tool to parse, with this you can search anything in your html.
NSRegularExpression Tutorial: Getting Started
I hope helped you with this explanation.
Updated syntax (swift 5) with a few modifications to get html data:
let url = NSURL(string: "https://www.stackoverflow.com")
let task = URLSession.shared.dataTask(with: url! as URL) {(data, response, error) in
if data != nil { // otherwise will crash for if url is empty
let dataString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)!
print("url data:",dataString)
} else {print("url data not found")}
}
task.resume()
Related
Edit: I was able to pin down the issue to a MUCH more concentrated field. Although this post here isn't necessarily wrong with its assumptions, Swift 4 base64 String to Data not working due to special character is much more clear and has a Playground example.
I have a string that has to be be serialized into a Dictionary in Swift 4. The app lets users upload data (JSON serialized as Data) and download it later. For the latter, the app does the following with the downloaded data (dlData)
if let rootDict = NSKeyedUnarchiver.unarchiveObject(with: dlData) as? Dictionary<String, Any> {
if let content = rootDict["C"] as? String {
if let data = content.data(using: .utf8, allowLossyConversion: true){
let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String:Any]
...
} else {
print("DATA DIDNT WORK") //gets printed with his data
}
Pretty much every time this has worked fine but recently a user has contacted me that on his iPhone there isn't any data showing up. I've added the else path and that's where it goes. It doesn't seem to be able to convert this particular string to Data
When I print() the string, copy the console output and then hardcode it into the method, it works just fine. The string is valid JSON (validated with 3 different online validators), and the JSONSerialization also works. But not in the "live" environment where it uses the downloaded data instead of the hardcoded print()-representation
Where I think the issue might be is that the Xcode console "cleans up" the string and "bad" characters that might be in it which is why copy-pasting makes it work but a direct download does not. The only weird thing I can spot in the print()ed string is the replacement character (the � symbol).
dlData > rootDict > content > data > json
Data > Dictionary > string > Data > Dictionary
Isn't the best possible chaining for this task but I am not in the position to change the infrastructure of this system. And because it does work for at least 95% of the users, I think it should work for all of them.
I've tried doing replacingOccurrences(of: "�", with: "?") but this doesn't affect the string, probably because in the actual string this is no "�" but something else and the "�" only gets displayed because the console doesn't know how else to put it.
I've come across this blog https://natrajbontha.wordpress.com/2017/10/12/replacement-character-in-json-data/ but I would actually prefer to do the cleaning up only when the conversion has already failed once.
The original character at this spot is the American flag emoji and my users could live without having the latest emojis in there but generally, I want emojis to be displayed so replacing all of them isn't a choice.
I've just tried the same in the Playground and it results in the same.
//b64String is dlData but in base64
let decodedData = Data(base64Encoded: b64String)! //works
if let unarchivedDictionary = NSKeyedUnarchiver.unarchiveObject(with: decodedData) as? Dictionary<String, Any> { //works
if let dF = unarchivedDictionary["C"] as? String { //works
print(dF) //prints
if let data = dF.data(using: .utf8, allowLossyConversion: true) { //fails
print(data)
} else {
print("NO DATA") //prints
}
}
}
I'm using the following code to extract weather information from the HTML of a website. The code tracks through the given URL and searches for content separated by the two phrases I've given as strings. The piece of information I need to extract is between two phrases but the first phrase has a line break in the HTML.
How do I represent this in the string? I've tried simply removing the line break and also using \n but this makes the fetch unsuccessful, as it can't find that phrase. I've represented the line break in my code as four asterisks.
I've also attached an image of the HTML I'm looking at. In this instance I'm trying to extract the time given in the HTML, but I also want to extract the 'Clear, cloudless sky' bit, which obviously will change regularly, as will some of the content preceding it.
The reason for needing the time is that I know this and the wind will change, and I want to ultimately extract the current conditions, so I'll have to tell the code to insert the correct time and wind to be able to fetch the current conditions.
if let url = attemptedURL {
let task = URLSession.shared.dataTask(with: url, completionHandler: { (data, response, error) -> Void in
if let urlContent = data {
let webContent = NSString(data: urlContent, encoding: String.Encoding.utf8.rawValue)
let websiteArray = webContent?.components(separatedBy: "<span class=\"current_description\">****<span>")
if websiteArray!.count > 1 {
let conditionArray = websiteArray![1].components(separatedBy: "</span>")
if conditionArray.count > 1 {
self.wasConSuccessful = true
let extract = conditionArray[0].replacingOccurrences(of: "°", with: "º")
DispatchQueue.main.async(execute: { () -> Void in
print(extract)
})
}
}
if self.wasConSuccessful == false {
self.conditionsLabel.text = "!"
Using SwiftSoup
do{
let doc: Document = try SwiftSoup.parse("<span class=\"current_description\"><span>5:30</span><span>3 km/h</span></span>")
let current_desc = try doc.getElementsByClass("current_description").first()
try print(current_desc?.select("span").get(1).text())//"5:30"
}catch Exception.Error(let type, let message){
print(message)
}catch{
print("error")
}
I have this JSON
{
"chatUsers":"[
{"id":"5","sender_id":"6","receiver_id":"1","content":"hi","datetime":"2016-11-19 00:00:00"},
{"id":"4","sender_id":"1","receiver_id":"2","content":"hello","datetime":"2016-11-11 00:00:00"},
{"id":"2","sender_id":"1","receiver_id":"3","content":"how are you","datetime":"2016-11-04 00:00:00"}
]",
"chatsCount":3
}
now I have this code to get data from the url :
let StringUrl = NSURL(string: url) as NSURL!
let Data = NSData(contentsOfURL: StringUrl) as NSData!
let ReadableData = JSON(data: Data)
let result = ReadableData["chatUsers"][0]["id"].string! as String // this should gives 5
but it always gives this error :
fatal error: unexpectedly found nil while unwrapping an Optional value
any idea why ?
There's quite a bit wrong with the example you provided above, but for the sake of getting you moving in the right direction, let me give you a some advice and a quick solution to your issue.
First off, don't start your variables with an uppercase character. Its not a set in stone rule or anything, but it's better practice to use camelCase as it will make your code more readable and avoid confusion.
Secondly, you are doing lots of force casting and force unwrapping of optional values which will make your application prone to crashes due to the same error you are getting now. I would take a look at the following for some better guidance: https://stackoverflow.com/a/25195633/4660602
Third, when posting on SO, please make sure you clearly give your question context as some users may believe you are asking a question about what optionals are and why you are getting the fatal error: unexpectedly found nil while unwrapping an Optional value error.
With that said, in the future please make sure to take a look at the SwiftyJSON docs as they pretty clearly demonstrate how to use the library. The reason your code is not working is because you are handling the JSON incorrectly. Here is an updated example:
let StringUrl = NSURL(string: url) as NSURL!
let Data = NSData(contentsOfURL: StringUrl) as NSData!
let ReadableData = JSON(data: Data)
let chatUsers = ReadableData["chatUsers"].arrayValue
let result = chatUsers[0]["id"].stringValue
or if you need to iterate through all the users:
let StringUrl = NSURL(string: url) as NSURL!
let Data = NSData(contentsOfURL: StringUrl) as NSData!
let ReadableData = JSON(data: Data)
for chatUser in ReadableData["chatUsers"]{
print(chatUser.1["id"].stringValue)
}
Please note I did not update your variable identifiers to the correct convention for the sake of not confusing you, but you should really follow the correct convention as I mentioned in my first point.
Good luck.
EDIT: Forgot to mention that your JSON is incorrectly formatted. Please fix your JSON data to the correct JSON format so that SwiftyJSON can correctly interpret it. You are receiving index out of range because SwiftyJSON does NOT know chatUsers is an array because the square brackets are wrapped in double quotes when they should not be. So
"chatUsers":"[
{"id":"5","sender_id":"6","receiver_id":"1","content":"hi","datetime":"2016-11-19 00:00:00"},
{"id":"4","sender_id":"1","receiver_id":"2","content":"hello","datetime":"2016-11-11 00:00:00"},
{"id":"2","sender_id":"1","receiver_id":"3","content":"how are you","datetime":"2016-11-04 00:00:00"}
]"
should be
"chatUsers": [
{"id":"5","sender_id":"6","receiver_id":"1","content":"hi","datetime":"2016-11-19 00:00:00"},
{"id":"4","sender_id":"1","receiver_id":"2","content":"hello","datetime":"2016-11-11 00:00:00"},
{"id":"2","sender_id":"1","receiver_id":"3","content":"how are you","datetime":"2016-11-04 00:00:00"}
]
i would ask a little help..
I have a function which try to get json data from a url. I am using SwiftyJSON.
this is my function:
func getDataFromUpc(Code: String) {
print("called getdata")
let urlString = "http://api.upcdatabase.org/json/mykey/" + Code
print(urlString)
let url = NSURL(string: urlString)
let data = NSData(contentsOfURL: url!)
let json = JSON(data!)
print(json)
print(json["description"])
self.alert(json["description"].stringValue)
}
when i try to print the json object on the console i can see this:
unknown
and when i try to print json["description"] it's nil.
If i try to call my url with my browser it's works and i get back the json response.
Could anybody please help me what am I doing wrong?
Thank you very much!
swiftyJSON don´t works with URL´s , swiftJSON works perfectly with local data
this is a tutorial that you can see to understand more the swiftJSON
https://www.youtube.com/watch?v=_NfijT6mt6A
to parse the JSON from the web you need to do that...
https://www.youtube.com/watch?v=r-LZs0De7_U
I am new to iOS development. I am using Swift and I have very little experience with Objective-C, so some of the other possibly related answers are tricky to understand. I am trying to understand how to use NSURLSession to get some data from a JSON file on the Web. I found some useful information about getting a file from a URL, but like this other StackOverflow user (NSURLSessionDataTask dataTaskWithURL completion handler not getting called), I heard that NSURLConnection was not the current way to get data, so I'm trying to use NSURLSession.
When I am getting my JSON from the bundle, I am using this extension to
Dictionary (I am pretty sure I got this code from a tutorial):
static func loadJSONFromBundle(filename: String) -> Dictionary<String, AnyObject>? {
let path = NSBundle.mainBundle().pathForResource(filename, ofType: ".json")
if !path {
println("Could not find level file: \(filename)")
return nil
}
var error: NSError?
let data: NSData? = NSData(contentsOfFile: path, options: NSDataReadingOptions(),
error: &error)
if !data {
println("Could not load level file: \(filename), error: \(error!)")
return nil
}
let dictionary: AnyObject! = NSJSONSerialization.JSONObjectWithData(data,
options: NSJSONReadingOptions(), error: &error)
if !dictionary {
println("Level file '\(filename)' is not valid JSON: \(error!)")
return nil
}
return dictionary as? Dictionary<String, AnyObject>
}
I'd like to do something similar for getting a dictionary from a JSON file that is on the web because I don't anticipate wanting to include all of my JSON files in the bundle. So far, I have this:
static func loadJSONFromWeb(urlstring: String) -> Dictionary<String, AnyObject>? {
let url = NSURL(string: urlstring)
let config = NSURLSessionConfiguration.defaultSessionConfiguration()
let session = NSURLSession(configuration: config, delegate: nil, delegateQueue: NSOperationQueue())
var error: NSError?
//I think I am using the completionHandler incorrectly. I'd like to access the data from the download
let task = session.downloadTaskWithRequest(NSURLRequest(URL: url), {(url, response, error) in println("The response is: \(response)")
})
task.resume()
//Isn't this contentsOfURL thing supposed to go with the connection stuff rather than the session stuff?
//How can I do this with a session? How can I create and use a completionHandler? This way seems clunky.
let data: NSData? = NSData(contentsOfURL: url)
if !data {
println("Could not load data from file: \(url), error: \(error!)")
return nil
}
println("The data is: \(data)")
let dictionary: AnyObject! = NSJSONSerialization.JSONObjectWithData(data,
options: NSJSONReadingOptions(), error: &error)
if !dictionary {
println("The file at '\(url)' is not valid JSON, error: \(error!)")
return nil
}
return dictionary as? Dictionary<String, AnyObject>
}
I think that my actual question that most needs answering is this: Where
is the data? I don't think I am using sessions and tasks correctly. I feel like I'm
starting a session to connect to a specific URL and using resume() to
start the download task I want to make happen, but I don't know how to
get the data from that JSON file.
If I need to use a completionHandler and a request in a way similar to what I found here:
(popViewControllerAnimated work slow inside NSURLSessionDataTask) can someone please explain how the 'data' in the completionHandler relates to the data in the fie I am trying to read/download? I am a bit baffled by the completionHandler and how to use it properly.
I looked at the documentation for NSData as well, but I didn't see anything that helped me understand how to get data from my session (or how to initialize an instance of NSData given my session). As far as I can tell form the documentation for NSURLDownloadTask, the task itself is not how I can access the data. It looks like the data comes from the session and task through the completionHandler.
EDIT:
I also looked at the documentation for NSURLSessionDownloadDelegate, but I could really use an example in Swift with some explanation about how to use the delegate. This led me to the URL Loading System Programming Guide. I'm guessing the benefits of using a session must be huge if the documentation is this complicated. I'm going to keep looking for more information on the URL Loading System.
I found this answer helpful (but I'm so new I can't upvote anything yet): https://stackoverflow.com/a/22177659/3842582 It helped me see that I am probably going to need to learn to use a delegate.
I also looked at the URL Loading System Programming Guide. I think what I really need is help with a completionHandler. How can I get my data? Or, am I already doing it correctly using NSData(contentsOfURL: url) (because I don't think I am).
Thank you in advance for any help you can offer!
First, let data: NSData? = NSData(contentsOfURL: url) will return your JSON synchronously. Did you try that and get this working simply? That way you can get started with the rest of your processing while figuring out NSURLSession.
If you're going to use NSURLSession, or a lot of other things in iOS, you need to learn delegates. Fortunately, they're easy. In terms of syntax you just add it to your class declaration like you were inheriting from it. What that does is say that you are going to implement at least the required functions for the delegate. These are callback functions which are generally pretty well documented. It is quite straightforward once you understand it.
If this is not a "heavyweight" project that really needs NSURLSession, you should look at this Swift library. Besides being a really nice way to deal with JSON there is a synchronous call to directly load the JSON from a url. https://github.com/dankogai/swift-json/
Why is NSURLConnection not the correct way to get data? You just should be careful with synchronous requests. Here is an example of how to get data from an url.
func synchronousExampleRequest() -> NSDictionary {
//creating the request
let url: NSURL! = NSURL(string: "http://exampledomain/apiexample.json")
var request = NSMutableURLRequest(URL: url)
request.HTTPMethod = "GET"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
var error: NSError?
var response: NSURLResponse?
let data = NSURLConnection.sendSynchronousRequest(request, returningResponse: &response, error: &error)
error = nil
let resultDictionary: NSDictionary = NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers, error: &error) as! NSDictionary
return resultDictionary
}