swiftswiftsoup

swift soup parsing with evaluateJavaScript in webview


I want to get a video URL from HTML -:

         <div class="_53mw" data-store="{&quot;videoID&quot;:&quot;607125377233758&quot;,&quot;playerFormat&quot;:&quot;inline&quot;,&quot;playerOrigin&quot;:&quot;permalink&quot;,&quot;external_log_id&quot;:null,&quot;external_log_type&quot;:null,&quot;rootID&quot;:607125377233758,&quot;playerSuborigin&quot;:&quot;misc&quot;,&quot;useOzLive&quot;:false,&quot;playbackIsLiveStreaming&quot;:false,&quot;canUseOffline&quot;:null,&quot;playOnClick&quot;:true,&quot;videoDebuggerEnabled&quot;:false,&quot;videoViewabilityLoggingEnabled&quot;:false,&quot;videoViewabilityLoggingPollingRate&quot;:-1,&quot;videoScrollUseLowThrottleRate&quot;:true,&quot;playInFullScreen&quot;:false,&quot;type&quot;:&quot;video&quot;,&quot;src&quot;:&quot;*https:\/\/video.fdel10-1.fna.fbcdn.net\/v\/t42.1790-2\/271574467_153621553687266_1119427332980623121_n.mp4?_nc_cat=106&amp;ccb=1-5&amp;_nc_sid=985c63&amp;efg=eyJ2ZW5jb2RlX3RhZyI6InN2ZV9zZCJ9&amp;_nc_ohc=HuufVpgJRnEAX8_AEix&amp;_nc_rml=0&amp;_nc_ht=video.fdel10-1.fna&amp;oh=00_AT8eUqTIMXRHidmafowZmL7-o4k2JG0FqA4QbFKNINiQ8Q&amp;oe=61DD4DFB&quot;,&quot;width&quot;:414,&quot;height&quot;:621*,&quot;trackingNodes&quot;:&quot;FH-R&quot;,&quot;downloadResources&quot;:null,&quot;subtitlesSrc&quot;:null,&quot;spherical&quot;:false,&quot;sphericalParams&quot;:null,&quot;defaultQuality&quot;:null,&quot;availableQualities&quot;:null,&quot;playStartSec&quot;:null,&quot;playEndSec&quot;:null,&quot;playMuted&quot;:null,&quot;disableVideoControls&quot;:false,&quot;loop&quot;:true,&quot;numOfLoops&quot;:13,&quot;shouldPlayInline&quot;:true,&quot;dashManifest&quot;:null,&quot;isAdsPreview&quot;:false,&quot;iframeEmbedReferrer&quot;:null,&quot;adClientToken&quot;:null,&quot;audioOnlyVideoSrc&quot;:null,&quot;audioOnlyEnabled&quot;:false,&quot;permalinkShareID&quot;:null,&quot;feedPosition&quot;:null,&quot;chainDepth&quot;:null,&quot;videoURL&quot;:&quot;https:\/\/www.facebook.com\/100069898026392\/videos\/607125377233758\/&quot;,&quot;disableLogging&quot;:false}" data-sigil="inlineVideo">
          

i am doing this-:

   do {
                let doc: Document = try SwiftSoup.parseBodyFragment(html)
                let headerTitle = try doc.title()

                // my body
                let body = doc.body()
                // elements to remove, in this case images
                let undesiredElements: Elements? = try body?.select("a")

                //remove
                try! undesiredElements?.remove()
               // print("Header title: \(headerTitle)")
                print("Header body: \(body)")
            } 
         

How to do this with swift soup


Solution

  • Try this code - :

     do {
                    let doc: Document = try SwiftSoup.parse(html)
                    let size = try doc.getElementsByClass("_53mw").first()
                    let data = try size?.attr("data-store")
                    print(data!)
                    let videoUrl = convertToDictionary(text: data!)
                    let url = videoUrl!["src"] as! String
                    print(url)
                
    
                } catch Exception.Error(let type, let message) {
                    print("Message: \(message)")
                } catch {
                    print("error")
                }