Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8804817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T01:42:16+00:00 2026-06-14T01:42:16+00:00

I am scraping some websites using BeautifulSoup and Requests. There is one page that

  • 0

I am scraping some websites using BeautifulSoup and Requests. There is one page that I am examining that has its data inside of a <script language="JavaScript" type="text/javascript"> tag. It looks like this:

<script language="JavaScript" type="text/javascript">
var page_data = {
   "default_sku" : "SKU12345",
   "get_together" : {
      "imageLargeURL" : "http://null.null/pictures/large.jpg",
      "URL" : "http://null.null/index.tmpl",
      "name" : "Paints",
      "description" : "Here is a description and it works pretty well",
      "canFavorite" : 1,
      "id" : 1234,
      "type" : 2,
      "category" : "faded",
      "imageThumbnailURL" : "http://null.null/small9.jpg"
       ......

Is there a way that I can create a python dictionary or json object out of the page_data variable within this script tag? That would be much nicer then trying to obtain values with BeautifulSoup.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T01:42:17+00:00Added an answer on June 14, 2026 at 1:42 am

    If you use BeautifulSoup to get the contents of the <script> tag, the json module can do the rest with a bit of string magic:

     jsonValue = '{%s}' % (textValue.partition('{')[2].rpartition('}')[0],)
     value = json.loads(jsonValue)
    

    The .partition() and .rpartition() combo above split the text on the first { and on the last } in the JavaScript text block, which should be your object definition. By adding the braces back to the text we can feed it to json.loads() and get a python structure from it.

    This works because JSON is basically the Javascript literal syntax objects, arrays, numbers, booleans and nulls.

    Demonstration:

    >>> import json
    >>> text = '''
    ... var page_data = {
    ...    "default_sku" : "SKU12345",
    ...    "get_together" : {
    ...       "imageLargeURL" : "http://null.null/pictures/large.jpg",
    ...       "URL" : "http://null.null/index.tmpl",
    ...       "name" : "Paints",
    ...       "description" : "Here is a description and it works pretty well",
    ...       "canFavorite" : 1,
    ...       "id" : 1234,
    ...       "type" : 2,
    ...       "category" : "faded",
    ...       "imageThumbnailURL" : "http://null.null/small9.jpg"
    ...    }
    ... };
    ... '''
    >>> json_text = '{%s}' % (text.partition('{')[2].rpartition('}')[0],)
    >>> value = json.loads(json_text)
    >>> value
    {'default_sku': 'SKU12345', 'get_together': {'imageLargeURL': 'http://null.null/pictures/large.jpg', 'URL': 'http://null.null/index.tmpl', 'name': 'Paints', 'description': 'Here is a description and it works pretty well', 'canFavorite': 1, 'id': 1234, 'type': 2, 'category': 'faded', 'imageThumbnailURL': 'http://null.null/small9.jpg'}}
    >>> import pprint
    >>> pprint.pprint(value)
    {'default_sku': 'SKU12345',
     'get_together': {'URL': 'http://null.null/index.tmpl',
                      'canFavorite': 1,
                      'category': 'faded',
                      'description': 'Here is a description and it works pretty '
                                     'well',
                      'id': 1234,
                      'imageLargeURL': 'http://null.null/pictures/large.jpg',
                      'imageThumbnailURL': 'http://null.null/small9.jpg',
                      'name': 'Paints',
                      'type': 2}}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using PhantomJS page.evaluate() to do some scraping. My problem is that the code
I am using nodeJS with some additional modules to do web page scraping and
I am trying to do some screen-scraping of a website. The content that I
I'm doing some screen scraping using WATIJ, but it can't read HTML tables (throws
I'm working on an app that does some web scraping. In the sites model,
So, I'm trying to do some screen scraping off of a certain site using
I got some pretty messy urls that i got via scraping here, problem is
I'm trying to scrape some data off of the FEC.gov website using python for
I'm using Mechanize for scraping images url then I'm looking http://mechanize.rubyforge.org/Mechanize/Page/Image.html for to know
I am scraping some information from a 10 year old website that was built

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.