Andy_ye
Andy_ye

Reputation: 570

Is there any way to get data from an javascript in html with python

This is the html script I get when I use requests.

<script type="text/javascript">
$(document).ready(function(){
        data = [{"str":"안녕하세요+저는+예상우+입니다","errInfo":[{"help":"입력 오류입니다.","errorIdx":0,"correctMethod":1,"start":9,"end":12,"orgStr":"예상우","candWord":"예상외"}],"idx":0}];
        pageIdx = 0;
        /*<![CDATA[*/
        if(1){
                totalPageCnt = 1;
        }
        /*]]>*/
        data = eval(data);

        makeHTML(0);

        if(totalPageCnt != 1){
                if(pageIdx == 0){
                        toast("총 " +totalPageCnt+"페이지입니다. 아래 화살표를 이용해 이동해주세요.");
                }

                document.getElementById('pageAnnounce').innerHTML = "총 " +totalPageCnt+"페이지 중 " + (pageIdx+1) +"페이지입니다.<br>화살표를 눌러 페이지를 이동해주세요.";
        }
});
</script>

I want to get str,help,errorIdx,correctMethod,start,end,orgStr,candWordfrom this javascript with python. How can I do this?

Upvotes: 0

Views: 148

Answers (1)

sushanth
sushanth

Reputation: 8302

Try this,

import re
import json
from bs4 import BeautifulSoup

soup = BeautifulSoup(text, "html.parser")

script = soup.find('script')
data = json.loads(re.search("data = (.*);", script.text).group(1))

print(data[0]['str'])

Upvotes: 1

Related Questions