Reputation: 77
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!
Upvotes: 0
Views: 951
Reputation: 1131
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)")
}
Upvotes: 2