0

I have a string like this:

{"index":11,"trackId":1394194,"isPaid":false,"tag":0,"title":"诗经_009周南_汉广","playCount":323093,"showLikeBtn":true,"isLike":false,"showShareBtn":true,"showCommentBtn":true,"showForwardBtn":true,"createDateFormat":"7年前","url":"/renwen/221727/1394194","duration":61,"isVideo":false,"videoCover":null,"isVipFirst":false,"breakSecond":0,"length":61}

Here is the info I want

诗经_009周南_汉广

My solution is

result = re.findall(r'\"title\":(.*?)\"(.*?)\"', str)

and result is

[ ('', '诗经_009周南_汉广')]

It start with title, tag is even better

do it greedy and least match to 广" , then get content in middle

How to do it better?

DNG
  • 599
  • 4
  • 14
  • 2
    Use `import json` and then `json.loads(s)["title"]` - https://ideone.com/UhJYvh – Wiktor Stribiżew Oct 30 '20 at 11:39
  • my situation is that , it is not simply json, it is json from webpage. I get this by `youtube-dl`, `webpage = self._download_webpage(url, audio_id, note='Download sound page for %s' % audio_id, errnote='Unable to get sound page')` – DNG Oct 30 '20 at 11:42
  • 2
    It is the same. You should "isolate", get the JSON using BeautifulSoup or other DOM parsing tool/library and then use JSON. – Wiktor Stribiżew Oct 30 '20 at 11:43
  • 1
    I strongly agree with @WiktorStribiżew. Don't re-implement what already exists, you will only make it worse (unless you really know what you are doing). – Finomnis Oct 30 '20 at 11:52
  • I agree also, https://ideone.com/ is nice – DNG Oct 30 '20 at 12:22

0 Answers0