0

I have some sample HTML here and I am trying to select the text inside of the <a> tag:

<div class="fsCalendarDaybox fsStateHasEvents">
<div class="fsCalendarDate" data-day="10" data-year="2019" data-month="0">
    <span class="fsCalendarDay">Thu,</span> 
    <span class="fsCalendarMonth">Jan</span>10
</div>
<div class="fsCalendarInfo">        
    <a class="fsCalendarEventTitle fsCalendarEventLink" title="D Day " data-occur-id="292598" href="#">D Day</a>
</div>

There are many more similar chucks on the website, and I need to be able to select the correct Letter Day (the value inside of <a> tag) based on an input day and month. Here is the Swift code that I am trying to use right now:

func parseData(dataToParse:String) -> String {
    let Month = 0
    let Day = 10
    do{
        let doc = try SwiftSoup.parse(dataToParse)
        do{
            let innerDiv = try doc.select("div.fsCalendarDate[data-day=\(Day)][data-month=\(Month)] + div.fsCalendarInfo")
            do{
                let a = try innerDiv.select("a.fsCalendarEventTitle")
                print(try a.text() as Any)
                if(try a.text() != nil){
                    return (try a.text())
                }
                else{
                    return "empty"
                }
            }
        }
    }
    catch{
        print("CANNOT PARSE WEBSITE DATA")
        return "ERROR"
    }
}

Unfortunately, I am getting all sorts of errors and I cannot consistently select the correct <div> element with the correct date. Anyone know what I am doing wrong?

  • I think I figured out my issue here (using commas messed everything up), but if anyone else has a more efficient way to select this element I would love to hear it! – Nolan Gelinas Jan 11 at 15:50

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.