Answer the question
In order to leave comments, you need to log in
How to parse json from html using python?
Good day to all.
There is one code that parses html using requests.get.
Next, from the resulting html, I need to get json ...
<script>
try{
try{
var card = JSON.parse('{"CardPAN":"1234567890123456789","EndDate":"01.01.2016","TicketTypeDesc":"00.04 CardName","CityName":"City","CardSum":99,"Time":"01.09.2016 11:11:01"}');
}
...
soup.find_all('try'))
and soup.find_all('var'))
so on - an empty value came out everywhere, obviously because I don’t catch up with how soup itself works, although I’m not flipping through the documentation for the first time. card.json()['CardSum']
, but this is only a theory. Answer the question
In order to leave comments, you need to log in
decoded = json.loads(html_body)
then put a regular dictionary
in html_body JSON, i.e.
html_body = '{"CardPAN":"1234567890123456789","EndDate":"01.01.2016","TicketTypeDesc":"00.04 CardName","CityName":"City","CardSum":99,"Time":"01.09.2016 11:11:01"}'
As I understand it - the problem is basically getting the string itself. There are regulars here.
Something like this:
for line in your_html_body_by_line:
res = re.match('.*JSON.parse\(\'(.*)\'\).*',line)
if res:
print json.loads(res.group(1))['CardPAN']
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question