Answer the question
In order to leave comments, you need to log in
How to properly parse data from bs4 ResultSet?
Good day.
Using the following line I get a ResultSet:
abc = soup.findAll('script', text = re.compile('Data'))
[<script type="text/javascript">
data = {"url":"haha.com", "id":"12345", "name":"haha",};
... function() {abc.devg....})'
...
Answer the question
In order to leave comments, you need to log in
I did not have time to test it 100%, the laptop was discharged. I'm writing from my phone.
import re
import json
from ast import literal_eval
pattern = re.compile(r'data[= ]+(?P<dict>.*);')
raw = pattern.search(s).groupdict()
# if валидый JSON
j = json.loads(raw['dict'])
print(j['id'], j['url'])
# запасной вариант
# elif похоже на валидный python-dict
d = literal_eval(raw['dict'])
print(d['id'], d['url'])
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question