【问题标题】:Parsing variable data out of a javascript tag using python使用python从javascript标签中解析变量数据
【发布时间】:2012-10-29 04:33:53
【问题描述】:

我正在使用 BeautifulSoup 和 Requests 抓取一些网站。我正在检查一个页面,其数据位于<script language="JavaScript" type="text/javascript"> 标记内。它看起来像这样:

<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"
       ......

有没有一种方法可以让我在这个脚本标签中的 page_data 变量中创建一个 python 字典或 json 对象?这会比尝试使用 BeautifulSoup 获取值要好得多。

【问题讨论】:

    标签: python html json beautifulsoup python-requests


    【解决方案1】:

    如果你使用 BeautifulSoup 来获取 &lt;script&gt; 标签的内容,json module 可以用一点字符串魔法来完成剩下的工作:

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

    上面的 .partition().rpartition() 组合将 JavaScript 文本块中第一个 { 和最后一个 } 上的文本分开,这应该是您的对象定义。通过将大括号添加回文本,我们可以将其提供给 json.loads() 并从中获取 python 结构。

    这是因为 JSON 基本上是 Javascript 文字语法对象、数组、数字、布尔值和空值。

    演示:

    >>> 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}}
    

    【讨论】:

    • 这真的很棒而且很有意义。感谢您对此的帮助。
    • 我很想知道如何将其重新用于不使用引号来表示对象键的对象声明,例如default_sku: "SKU12345", ...。它可能只需要一个正则表达式......
    • @2rs2ts:请参阅 Issue with html tags while scraping data using beautiful soup 以获取先前添加引号以使 JSON 有效的答案。
    • 虽然该正则表达式对我不起作用,但我发现使用正则表达式绝对是合适的方法。谢谢你:)
    • 是的,这个正则表达式有些具体;它不允许在开头的{ 或逗号之间使用空格。它还假设在任何地方的字符串值中都没有左大括号和逗号。添加一些空白余量 (\s*) 并保持关于 {, 未出现在值中的假设,并且您应该能够使用正则表达式将 Javascript 对象转换为 JSON。
    猜你喜欢
    • 2014-05-01
    • 2021-10-07
    • 1970-01-01
    • 1970-01-01
    • 2013-08-24
    • 2013-01-08
    • 2014-07-16
    相关资源
    最近更新 更多