Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python从javascript标记解析变量数据_Python_Html_Json_Beautifulsoup_Python Requests - Fatal编程技术网

使用python从javascript标记解析变量数据

使用python从javascript标记解析变量数据,python,html,json,beautifulsoup,python-requests,Python,Html,Json,Beautifulsoup,Python Requests,我正在使用BeautifulSoup和请求删除一些网站。我正在检查一个页面,它的数据在标记中。看起来是这样的: <script language="JavaScript" type="text/javascript"> var page_data = { "default_sku" : "SKU12345", "get_together" : { "imageLargeURL" : "http://null.null/pictures/large.jpg",

我正在使用BeautifulSoup和请求删除一些网站。我正在检查一个页面,它的数据在
标记中。看起来是这样的:

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

变量页_数据={
“默认sku”:“SKU12345”,
“聚在一起”:{
“imageLargeURL”:http://null.null/pictures/large.jpg",
“URL”:”http://null.null/index.tmpl",
“名称”:“油漆”,
“描述”:“这是一个描述,它工作得很好”,
“canFavorite”:1,
“id”:1234,
“类型”:2,
“类别”:“褪色”,
“imageThumbnailURL”:http://null.null/small9.jpg"
......

有没有一种方法可以从这个脚本标记中的
page\u data
变量中创建python字典或json对象?这比尝试使用BeautifulSoup获取值要好得多。

如果使用BeautifulSoup获取
标记的内容,则可以使用一点字符串魔法来完成其余的工作:

 jsonValue = '{%s}' % (textValue.partition('{')[2].rpartition('}')[0],)
 value = json.loads(jsonValue)
上面的
.partition()
.rpartition()
组合在JavaScript文本块的第一个
{
和最后一个
}
上拆分文本,这应该是您的对象定义。通过将大括号添加回文本,我们可以将其反馈到文本并从中获取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\u sku:“SKU12345”,…
。可能只需要一个正则表达式…@2rs2ts:请参阅前面的答案,其中添加了引号以使JSON有效。虽然该正则表达式不适用于我,但我认为使用正则表达式绝对是合适的方法。谢谢:)是的,该正则表达式有点特殊;它不允许开头之间出现空格
{
或逗号。它还假设字符串值中的任何位置都没有开头的大括号和逗号。添加一些空格容差(
\s*
)并保持关于
{
不出现在值中,您应该能够使用正则表达式将Javascript对象转换为JSON。