Q:

How to get extract dictionary embedded in a 

I am trying to scrape the zip codes:shipping pairs from a portion of text within a  within a div.
I've tried to use .find to get the div and then .find again to get the  followed by a pattern to pass for the portion of the text I'd like the extraction to start from.
from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup
import re
import json

my_url = 'https://www.amazon.com/Prime-FREE-Same-Day-Delivery/b?ie=UTF8&node=8729023011'

divcontainer = page_soup.find('div', attrs={'id' : 'top'})

script = divcontainer.find("script").text

pattern = re.compile(r'postalToProgramIDHash \"(.*?)\"', re.MULTILINE)

zips = re.search(pattern, script)

portion of script I want to extract:
 'postalToProgramIdHash':  {"94087":"free_same_day","11412":"free_same_day","93227":"free_same_day","75227":"free_same_day"..."84130":"free_same_day","40233":"free_same_day","27717":"free_same_day","45202":"free_same_day"},
    'marketplaceId'

I expect a dictionary with zip code:shipping key:value pairs as a result.

A:

Try pattern re.compile(r"'postalToProgramIdHash':\s*({.*?})", flags=re.MULTILINE)
Ex:
import re
import json

script = """'postalToProgramIdHash':  {"94087":"free_same_day","11412":"free_same_day","93227":"free_same_day","75227":"free_same_day", "84130":"free_same_day","40233":"free_same_day","27717":"free_same_day","45202":"free_same_day"},
    'marketplaceId'"""

pattern = re.compile(r"'postalToProgramIdHash':\s*({.*?})", flags=re.MULTILINE)

zips = re.search(pattern, script)
print(json.loads(zips.group(1)))

Output:
{u'11412': u'free_same_day',
 u'27717': u'free_same_day',
 u'40233': u'free_same_day',
 u'45202': u'free_same_day',
 u'75227': u'free_same_day',
 u'84130': u'free_same_day',
 u'93227': u'free_same_day',
 u'94087': u'free_same_day'}

