Eduardo M Eduardo M - 22 days ago 5
Python Question

How to get information from a dictionary inside a tag in a webpage?

For example, this tag has a dictionary inside itself:

<script>var ytplayer = ytplayer || {};ytplayer.config = {"min_version":"8.0.0","url_v9as2":"","html5":true,"url":"https:\/\/s.ytimg.com\/yts\/swfbin\/player-vfl0vuWtf\/watch_as3.swf","params":{"allowfullscreen":"true","allowscriptaccess":"always","bgcolor":"#000000"},"url_v8":"https:\/\/s.ytimg.com\/yts\/swfbin\/player-vfl0vuWtf\/cps.swf","messages":{"player_fallback":["É necessário usar um navegador com Adobe Flash Player ou HTML5 para reproduzir o vídeo. \u003ca href=\"https:\/\/get.adobe.com\/flashplayer\/\"\u003eFaça o download do Flash Player mais recente\u003c\/a\u003e\u003ca href=\"\/html5\"\u003eSaiba mais sobre como atualizar para um navegador com HTML5\u003c\/a\u003e"]},"sts":17121,"args":{"atc":"a=3\u0026b=y00NWXXJT4pawoKWlj6OErUoQK0\u0026c=1479471731\u0026d=1\u0026e=mTFTEBOiQKg\u0026c3a=23\u0026c1a=1\u0026c6a=1\u0026hh=b40VIK_c4NgYZ0AHKwGfIx4XVus","default_audio_track_index":"0","dbp":"Ch4KFlBvczhZLTFqTUhlSU15ZEF6TlpFNEEQASABKAAQARgC","c":"WEB","iv3_module":"1","url_encoded_fmt_stream_map":"quality=hd720\u0026itag=22\u0026type=video%2Fmp4%3B+codecs%3D%22avc1.64001F%2C+mp4a.40.2%22\u0026url=https%3A%2F%2Fr3---sn-oxunxg8pjvn-gxje.googlevideo.com%2Fvideoplayback%3Fkey%3Dyt6%26mime%3Dvideo%252Fmp4%26ei%3Dc_IuWNTnMMOa-wXJ3J2oAQ%26expire%3D1479493331%26upn%3Di8FZTAJ60wc%26signature%3D1C4D0BDA83614B78035BDF2A7F183890F710C77D.934E6081E929FCF244C8DD102B1A941F240FF534%26pcm2cms%3Dyes%26initcwndbps%3D1505000%26ratebypass%3Dyes%26dur%3D1658.299%26lmt%3D1469701537595813%26source%3Dyoutube%26sparams%3Ddur%252Cei%252Cid%252Cinitcwndbps%252Cip%252Cipbits%252Citag%252Clmt%252Cmime%252Cmm%252Cmn%252Cms%252Cmv%252Cpcm2cms%252Cpl%252Cratebypass%252Crequiressl%252Csource%252Cupn%252Cexpire%26requiressl%3Dyes%26mn%3Dsn-oxunxg8pjvn-gxje%26itag%3D22%26mm%3D31%26ip%3D191.179.99.51%26ipbits%3D0%26id%3Do-AEVN0QsDhSs-posfdMY-EcmDqiFZLDIGmVArlsUXKjLW%26pl%3D26%26ms%3Dau%26mv%3Dm%26mt%3D1479471623,quality=medium\u0026itag=43\u0026type=video%2Fwebm%3B+codecs%3D%22vp8.0%2C+vorbis%22\u0026url=https%3A%2F%2Fr3---sn-oxunxg8pjvn-gxje.googlevideo.com%2Fvideoplayback%3Fkey%3Dyt6%26mime%3Dvideo%252Fwebm%26ei%3Dc_IuWNTnMMOa-wXJ3J2oAQ%26expire%3D1479493331%26upn%3Di8FZTAJ60wc%26signature%3D93C02ED2185AF497566092AF5BEE9DC98B04E088.13519BB26CF8FB376A55D498B55672D1AE46D2C4%26pcm2cms%3Dyes%26initcwndbps%3D1505000%26clen%3D123527907%26gir%3Dyes%26ratebypass%3Dyes%26dur%3D0.000%26lmt%3D1469584815147271%26source%3Dyoutube%26sparams%3Dclen%252Cdur%252Cei%252Cgir%252Cid%252Cinitcwndbps%252Cip%252Cipbits%252Citag%252Clmt%252Cmime%252Cmm%252Cmn%252Cms%252Cmv%252Cpcm2cms%252Cpl%252Cratebypass%252Crequiressl%252Csource%252Cupn%252Cexpire%26requiressl%3Dyes%26mn%3Dsn-oxunxg8pjvn-gxje%26itag%3D43%26mm%3D31%26ip%3D191.179.99.51%26ipbits%3D0%26id%3Do-AEVN0QsDhSs-posfdMY-EcmDqiFZLDIGmVArlsUXKjLW%26pl%3D26%26ms%3Dau%26mv%3Dm%26mt%3D1479471623,quality=medium\u0026itag=18\u0026type=video%2Fmp4%3B+codecs%3D%22avc1.42001E%2C+mp4a.40.2%22\u0026url=https%3A%2F%2Fr3---sn-oxunxg8pjvn-gxje.googlevideo.com%2Fvideoplayback%3Fkey%3Dyt6%26mime%3Dvideo%252Fmp4%26ei%3Dc_IuWNTnMMOa-wXJ3J2oAQ%26expire%3D1479493331%26upn%3Di8FZTAJ60wc%26signature%3D1B8F26FEE81EA5FC0EDB6863B1D43B6E144833D3.69699588E90B6A93C09117C4FE315BFF4390A939%26pcm2cms%3Dyes%26initcwndbps%3D1505000%26clen%3D88217937%26gir%3Dyes%26ratebypass%3Dyes%26dur%3D1658.299%26lmt%3D1469699862807497%26source%3Dyoutube%26sparams%3Dclen%252Cdur%252Cei%252Cgir%252Cid%252Cinitcwndbps%252Cip%252Cipbits%252Citag%252Clmt%252Cmime%252Cmm%252Cmn%252Cms%252Cmv%252Cpcm2cms%252Cpl%252Cratebypass%252Crequiressl%252Csource%252Cupn%252Cexpire%26requiressl%3Dyes%26mn%3Dsn-oxunxg8pjvn-gxje%26itag%3D18%26mm%3D31%26ip%3D191.179.99.51%26ipbits%3D0%26id%3Do-AEVN0QsDhSs-posfdMY-EcmDqiFZLDIGmVArlsUXKjLW%26pl%3D26%26ms%3Dau%26mv%3Dm%26mt%3D1479471623,quality=small\u0026itag=36\u0026type=video%2F3gpp%3B+codecs%3D%22mp4v.20.3%2C+mp4a.40.2%22\u0026url=https%3A%2F%2Fr3---sn-oxunxg8pjvn-gxje.googlevideo.com%2Fvideoplayback%3Fkey%3Dyt6%26mime%3Dvideo%252F3gpp%26ei%3Dc_IuWNTnMMOa-wXJ3J2oAQ%26expire%3D1479493331%26upn%3Di8FZTAJ60wc%26signature%3D09268BE0A45C314F05AB796D67BC87BEE8163D9E.22D799173EA0D50938640E172B77AE5BC7F3350E%26pcm2cms%3Dyes%26initcwndbps%3D1505000%26clen%3D46898538%26gir%3Dyes%26dur%3D1658.369%26lmt%3D1469583142241685%26source%3Dyoutube%26sparams%3Dclen%252Cdur%252Cei%252Cgir%252Cid%252Cinitcwndbps%252Cip%252Cipbits%252Citag%252Clmt%252Cmime%252Cmm%252Cmn%252Cms%252Cmv%252Cpcm2cms%252Cpl%252Crequiressl%252Csource%252Cupn%252Cexpire%26requiressl%3Dyes%26mn%3Dsn-oxunxg8pjvn-gxje%26itag%3D36%26mm%3D31%26ip%3D191.179.99.51%26ipbits%3D0%26id%3Do-AEVN0QsDhSs-posfdMY-EcmDqiFZLDIGmVArlsUXKjLW%26pl%3D26%26ms%3Dau%26mv%3Dm%26mt%3D1479471623,quality=small\u0026itag=17\u0026type=video%2F3gpp%3B+codecs%3D%22mp4v.20.3%2C+mp4a.40.2%22\u0026url=https%3A%2F%2Fr3---sn-oxunxg8pjvn-gxje.googlevideo.com%2Fvideoplayback%3Fkey%3Dyt6%26mime%3Dvideo%252F3gpp%26ei%3Dc_IuWNTnMMOa-wXJ3J2oAQ%26expire%3D1479493331%26upn%3Di8FZTAJ60wc%26signature%3D2B844912D9E5A2C3F574C46EC1B793AB54AF399D.18879D23BF58C663894CDEDE5B07B469D92867DB%26pcm2cms%3Dyes%26initcwndbps%3D1505000%26clen%3D17027962%26gir%3Dyes%26dur%3D1658.369%26lmt%3D1469583148844645%26source%3Dyoutube%26sparams%3Dclen%252Cdur%252Cei%252Cgir%252Cid%252Cinitcwndbps%252Cip%252Cipbits%252Citag%252Clmt%252Cmime%252Cmm%252Cmn%252Cms%252Cmv%252Cpcm2cms%252Cpl%252Crequiressl%252Csource%252Cupn%252Cexpire%26requiressl%3Dyes%26mn%3Dsn-oxunxg8pjvn-gxje%26itag%3D17%26mm%3D31%26ip%3D191.179.99.51%26ipbits%3D0%26id%3Do-AEVN0QsDhSs-posfdMY-EcmDqiFZLDIGmVArlsUXKjLW%26pl%3D26%26ms%3Dau%26mv%3Dm%26mt%3D1479471623","caption_tracks":"n=Ingl%C3%AAs+%28gerada+automaticamente%29\u0026k=asr\u0026lc=en\u0026v=a.en\u0026u=https%3A%2F%2Fwww.youtube.com%2Fapi%2Ftimedtext%3Fkey%3Dyttt1%26expire%3D1479496931%26sparams%3Dasr_langs%252Ccaps%252Cv%252Cexpire%26caps%3Dasr%26signature%3D289E75E9407B7D62466E8A54146F1FE962CCF3A8.E89AD5D86F34FAF955C7C68EF0187CAFF296B3B0%26v%3DmTFTEBOiQKg%26hl%3Dpt_BR%26asr_langs%3Dko%252Cit%252Cfr%252Cpt%252Cen%252Cnl%252Cja%252Ces%252Cru%252Cde%26kind%3Dasr%26lang%3Den\u0026t=1,n=Portugu%C3%AAs\u0026u=https%3A%2F%2Fwww.youtube.com%2Fapi%2Ftimedtext%3Fkey%3Dyttt1%26expire%3D1479496931%26sparams%3Dasr_langs%252Ccaps%252Cv%252Cexpire%26caps%3Dasr%26signature%3D289E75E9407B7D62466E8A54146F1FE962CCF3A8.E89AD5D86F34FAF955C7C68EF0187CAFF296B3B0%26v%3DmTFTEBOiQKg%26hl%3Dpt_BR%26asr_langs%3Dko%252Cit%252Cfr%252Cpt%252Cen%252Cnl%252Cja%252Ces%252Cru%252Cde%26lang%3Dpt\u0026lc=pt\u0026t=1\u0026v=.pt","afv":true,"ad3_module":"1","ucid":"UCg9g2JnnU_6rkPLHuFxfCBw","iurlmaxres_webp":"https:\/\/i.ytimg.com\/vi_webp\/mTFTEBOiQKg\/maxresdefault.webp","fade_out_start_milliseconds":"0","tag_for_child_directed":false,"probe_url":"https:\/\/r5---sn-a5m7lnlz.googlevideo.com\/videogoodput?id=o-AJsGyC42L0Wp4IK3B8GXACPHni2yMAwrrtLw_eFmeM7u\u0026source=goodput\u0026range=0-4999\u0026expire=1479475331\u0026ip=191.179.99.51\u0026ms=pm\u0026mm=35\u0026pl=24\u0026nh=IgpwZjAxLmxheDI4KgwxNDQuMjIzLjQ0LjY\u0026sparams=id,source,range,expire,ip,ms,mm,pl,nh\u0026signature=800AF7866E7CF4CDBF52CA44A770BA1C6FE0649B.26CC388902036D09FC9FF795DD3B9E3BC5AAC46A\u0026key=cms1","no_get_video_log":"1","user_age":"39","hl":"pt_BR","fade_in_start_milliseconds":"-3000","plid":"AAVBklsJmwroFEdM","tmi":"1","iv_load_policy":"1","csi_page_type":"watch,watch7ad_html5","keywords":"","apiary_host":"","vm":"CAEQAQ","avg_rating":"4.95011329651","cver":"1.20161117","ad_logging_flag":"1","gut_tag":"\/4061\/ytpwatch","iv_module":"https:\/\/s.ytimg.com\/yts\/swfbin\/player-vfl0vuWtf\/iv_module.swf","adaptive_fmts":"url=https%3A%2F%2Fr3---sn-oxunxg8pjvn-gxje.googlevideo.com%2Fvideoplayback%3Fkey%3Dyt6%26mime%3Dvideo%252Fmp4%26ei%3Dc_IuWNTnMMOa-wXJ3J2oAQ%26expire%3D1479493331%26upn%3Di8FZTAJ60wc%26signature%3D6E1B4A242EF1D84D12A3B5E6E6F48899C037474F.627021EDF19076A8C0AB5BD061DA500C83A23F4C%26pcm2cms%3Dyes%26initcwndbps%3D1505000%26clen%3D199526287%26gir%3Dyes%26dur%3D1658.156%26lmt%3D1469701227364554%26source%3Dyoutube%26sparams%3Dclen%252Cdur%252Cei%252Cgir%252Cid%252Cinitcwndbps%252Cip%252Cipbits%252Citag%252Ckeepalive%252Clmt%252Cmime%252Cmm%252Cmn%252Cms%252Cmv%252Cpcm2cms%252Cpl%252Crequiressl%252Csource%252Cupn%252Cexpire%26requiressl%3Dyes%26mn%3Dsn-oxunxg8pjvn-gxje%26itag%3D136%26mm%3D31%26ip%3D191.179.99.51%26ipbits%3D0%26id%3Do-AEVN0QsDhSs-posfdMY-EcmDqiFZLDIGmVArlsUXKjLW%26pl%3D26%26keepalive%3Dyes%26ms%3Dau%26mv%3Dm%26mt%3D1479471623\u0026fps=30\u0026projection_type=1\u0026type=video%2Fmp4%3B+codecs%3D%22avc1.4d401f%22\u0026size=1280x720\u0026quality_label=720p\u0026itag=136\u0026clen=199526287\u0026index=713-4476\u0026lmt=1469701227364554\u0026init=0-712\u0026xtags=\u0026bitrate=1122649,url=https%3A%2F%2Fr3---sn-oxunxg8pjvn-gxje.googlevideo.com%2Fvideoplayback%3Fkey%3Dyt6%26mime%3Dvideo%252Fwebm%26ei%3Dc_IuWNTnMMOa-wXJ3J2oAQ%26expire%3D1479493331%26upn%3Di8FZTAJ60wc%26signature%3D643A1ECD215EABDD7FADD031FE18FD99D1751448.27FAEBFCCAA49DD0E51D099C86322B1E16DEAF10%26pcm2cms%3Dyes%26initcwndbps%3D1505000%26clen%3D184202675%26gir%3Dyes%26dur%3D1658.122%26lmt%3D1469586259159785%26source%3Dyoutube%26sparams%3Dclen%252Cdur%252Cei%252Cgir%252Cid%252Cinitcwndbps%252Cip%252Cipbits%252Citag%252Ckeepalive%252Clmt%252Cmime%252Cmm%252Cmn%252Cms%252Cmv%252Cpcm2cms%252Cpl%252Crequiressl%252Csource%252Cupn%252Cexpire%26requiressl%3Dyes%26mn%3Dsn-oxunxg8pjvn-gxje%26itag%3D247%26mm%3D31%26ip%3D191.179.99.51%26ipbits%3D0%26id%3Do-AEVN0QsDhSs-posfdMY-EcmDqiFZLDIGmVArlsUXKjLW%26pl%3D26%26keepalive%3Dyes%26ms%3Dau%26mv%3Dm%26mt%3D1479471623\u0026fps=30\u0026projection_type=1\u0026type=video%2Fwebm%3B+codecs%3D%22vp9%22\u0026size=1280x720\u0026quality_label=720p\u0026itag=247\u0026clen=184202675\u0026index=243-5791\u0026lmt=1469586259159785\u0026init=0-242\u0026xtags=\u0026bitrate=1361715,url=https%3A%2F%2Fr3---sn-oxunxg8pjvn-gxje.googlevideo.com%2Fvideoplayback%3Fkey%3Dyt6%26mime%3Dvideo%252Fmp4%26ei%3Dc_IuWNTnMMOa-wXJ3J2oAQ%26expire%3D1479493331%26upn%3Di8FZTAJ60wc%26signature%3D24C2A06CE95A…</script>


And I want to get some informations from this dictionary (url values from "url_enconded_fmt_stream_map" key). But I have no idea how to do that, even though I have already searched it a lot.

EDIT

The tag is not full, I don't know why (I copied it directly from the site (YouTube)).

Following Alex Hall's code with some changes , I got this result:

Code:

with open('script.txt') as infile:
for _, __, obj in jsonfinder(infile.read(), json_only=True):
print(obj)


Output:

{}
{'allowfullscreen': 'true', 'bgcolor': '#000000', 'allowscriptaccess': 'always'}
{'player_fallback': ['É necessário usar um navegador com Adobe Flash Player ou HTML5 para reproduzir o vídeo. <a href="https://get.adobe.com/flashplayer/">Faça o download do Flash Player mais recente</a><a href="/html5">Saiba mais sobre como atualizar para um navegador com HTML5</a>']}


But even searching for 'allowfullscreen' key, it returns a KeyError.

Answer

The value is encoded in JSON and thus can be extracted with the library jsonfinder, e.g. like this:

from jsonfinder import jsonfinder

with open('script.txt') as infile:
    for _, __, obj in jsonfinder(infile.read(), json_only=True):
        try:
            print(obj['args']['url_encoded_fmt_stream_map'])
        except KeyError:
            pass