Q:

How to generate a dict from string separated by semicolon?

I am using Beautifulsoup to search for a specific number in HTML but got stuck here.
The raw data is:

<div class='box_content' hn_bookmark='true' ng_init=" bookmarked=false; bookmark_id=''; bookmarks_path='/en-US/bookmarks'; bookmarkable_id='000000'; bookmarkable_type=''; ">

and I want to extract the "bookmarkable_id".
bsobj = BeautifulSoup(text,"html.parser")
questionID_line = bsobj.find("div",{"class":"box_content"})['ng_init']

It returns to me a string with words separated by semicolons:
bookmarked=false; bookmark_id=''; bookmarks_path='/en-US/bookmarks'; bookmarkable_id='793447'; bookmarkable_type='Question'

But I don't know how I can extract from here. Please help!

A:

Try this:
data = "bookmarked=false; bookmark_id=''; bookmarks_path='/en-US/bookmarks'; bookmarkable_id='793447'; bookmarkable_type='Question'"

fields = {}
for f in data.split('; '):
    k , v = f.split('=')
    fields[k] = v.strip("'")

print(fields)

Gives:
{'bookmarked': 'false', 'bookmark_id': '', 'bookmarks_path': '/en-US/bookmarks', 'bookmarkable_type': 'Question', 'bookmarkable_id': '793447'}

