0

Is there a method to parse an HTML link for tags with specific attributes?

Right now I use the following code to get the content of the page (with SwiftSoup):

    // Checking if MyLink is working
    guard let myURL = URL(string: MyLink) else {                                                 // myURL needed only temporarly
        print("Error: \(MyLink) doesn't seem to be a valid URL")
        return
    }

    // Getting all content of website for a specific link
    do {
        MyLinkContent = try String(contentsOf: myURL, encoding: .ascii)
    } catch let error {
        print("Error: \(error)")
    }

After I get the content I can save all links in an array, for example with this code:

        // Searching for all links in the content of the URL and creating elements
        guard let linkElements: Elements = try? SwiftSoup.parse(MyLinkContent).select("a")  else {return}
        //  Now all elements are printed into an array
        for element: Element in linkElements.array(){
            MyLinkArray.append("\(element)")
        }

This results in an array with all links of the website.

My question however is, say for example the homepage has a lot of tags with the following format:

- Things I don't need especially a lot of <tr tags but with other attributes I don't need
- <tr id="row_">"TheContentINeed"</tr>
- <tr id="row_">"TheContentINeed"</tr> 
- <tr id="row_">"TheContentINeed"</tr> 
- <tr id="row_">"TheContentINeed"</tr> 
- <tr id="row_">"TheContentINeed"</tr> 
- More Things I don't need specially a lot of <tr tags but with other attributes I don't need

I would like to save all contents in an array, but only the tags with the attribute "id"

Any ideas? I would prefer to use SwiftSoup (if possible) again. Thanks!

1 Answer 1

2

Combine your CSS selectors. See Example project from more.

// Searching for all links in the content of the URL and creating elements
guard let linkElements: Elements = try? SwiftSoup.parse(MyLinkContent).select("a tr[id="row"]")  else {return}
//  Now all elements are printed into an array
for element: Element in linkElements.array(){
   MyLinkArray.append("\(element)")
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot I will try it in the next days and keep you updated!

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.