gpt4 book ai didi

python - 如何在python中解析wordpress分类数据库内容

转载 作者:太空宇宙 更新时间:2023-11-03 15:45:15 24 4
gpt4 key购买 nike

这是 WordPress 数据库中 postmeta 表的行转储

如何在 python 中解析 wordpress 之外的数据。

a:17:{s:13:"pa_tea-coffee";a:6:{s:4:"name";s:13:"pa_tea-coffee";s:5:"value";s:0:"";s:8:"position";s:1:"0";s:10:"is_visible";i:1;s:12:"is_variation";i:0;s:11:"is_taxonomy";i:1;}s:24:"pa_bottled-mineral-water";a:6:{s:4:"name";s:24:"pa_bottled-mineral-water";s:5:"value";s:0:"";s:8:"position";s:1:"1";s:10:"is_visible";i:1;s:12:"is_variation";i:0;s:11:"is_taxonomy";i:1;}s:17:"pa_drinking-water";a:6:{s:4:"name";s:17:"pa_drinking-water";s:5:"value";s:0:"";s:8:"position";s:1:"2";s:10:"is_visible";i:1;s:12:"is_variation";i:0;s:11:"is_taxonomy";i:1;}s:8:"pa_wi-fi";a:6:{s:4:"name";s:8:"pa_wi-fi";s:5:"value";s:0:"";s:8:"position";s:1:"3";s:10:"is_visible";i:1;s:12:"is_variation";i:0;s:11:"is_taxonomy";i:1;}s:7:"pa_cctv";a:6:{s:4:"name";s:7:"pa_cctv";s:5:"value";s:0:"";s:8:"position";s:1:"4";s:10:"is_visible";i:1;s:12:"is_variation";i:0;s:11:"is_taxonomy";i:1;}s:20:"pa_security-services";a:6:{s:4:"name";s:20:"pa_security-services";s:5:"value";s:0:"";s:8:"position";s:1:"5";s:10:"is_visible";i:1;s:12:"is_variation";i:0;s:11:"is_taxonomy";i:1;}s:27:"pa_fire_fighting_equipmment";a:6:{s:4:"name";s:27:"pa_fire_fighting_equipmment";s:5:"value";s:0:"";s:8:"position";s:1:"6";s:10:"is_visible";i:1;s:12:"is_variation";i:0;s:11:"is_taxonomy";i:1;}s:25:"pa_fire-alarms-sprinklers";a:6:{s:4:"name";s:25:"pa_fire-alarms-sprinklers";s:5:"value";s:0:"";s:8:"position";s:1:"7";s:10:"is_visible";i:1;s:12:"is_variation";i:0;s:11:"is_taxonomy";i:1;}s:21:"pa_reception-services";a:6:{s:4:"name";s:21:"pa_reception-services";s:5:"value";s:0:"";s:8:"position";s:1:"8";s:10:"is_visible";i:1;s:12:"is_variation";i:0;s:11:"is_taxonomy";i:1;}s:24:"pa_housekeeping-services";a:6:{s:4:"name";s:24:"pa_housekeeping-services";s:5:"value";s:0:"";s:8:"position";s:1:"9";s:10:"is_visible";i:1;s:12:"is_variation";i:0;s:11:"is_taxonomy";i:1;}s:17:"pa_centralized-ac";a:6:{s:4:"name";s:17:"pa_centralized-ac";s:5:"value";s:0:"";s:8:"position";s:2:"10";s:10:"is_visible";i:1;s:12:"is_variation";i:0;s:11:"is_taxonomy";i:1;}s:23:"pa_multiple-powerpoints";a:6:{s:4:"name";s:23:"pa_multiple-powerpoints";s:5:"value";s:0:"";s:8:"position";s:2:"11";s:10:"is_visible";i:1;s:12:"is_variation";i:0;s:11:"is_taxonomy";i:1;}s:23:"pa_train-station-access";a:6:{s:4:"name";s:23:"pa_train-station-access";s:5:"value";s:0:"";s:8:"position";s:2:"12";s:10:"is_visible";i:1;s:12:"is_variation";i:0;s:11:"is_taxonomy";i:1;}s:14:"pa_ups-back-up";a:6:{s:4:"name";s:14:"pa_ups-back-up";s:5:"value";s:0:"";s:8:"position";s:2:"13";s:10:"is_visible";i:1;s:12:"is_variation";i:0;s:11:"is_taxonomy";i:1;}s:20:"pa_4-wheeler-parking";a:6:{s:4:"name";s:20:"pa_4-wheeler-parking";s:5:"value";s:0:"";s:8:"position";s:2:"14";s:10:"is_visible";i:1;s:12:"is_variation";i:0;s:11:"is_taxonomy";i:1;}s:20:"pa_2-wheeler-parking";a:6:{s:4:"name";s:20:"pa_2-wheeler-parking";s:5:"value";s:0:"";s:8:"position";s:2:"15";s:10:"is_visible";i:1;s:12:"is_variation";i:0;s:11:"is_taxonomy";i:1;}s:17:"pa_public-parking";a:6:{s:4:"name";s:17:"pa_public-parking";s:5:"value";s:0:"";s:8:"position";s:2:"16";s:10:"is_visible";i:1;s:12:"is_variation";i:0;s:11:"is_taxonomy";i:1;}}

无法使用 python - phpserialize 包来做到这一点

错误引用

Traceback (most recent call last):
File "/home/cj/projects/scripts/usageTypes.py", line 53, in <module>
products = getProducts()
File "/home/cj/projects/scripts/usageTypes.py", line 50, in getProducts
products = build_product_dict(products)
File "/home/cj/projects/scripts/usageTypes.py", line 44, in build_product_dict
products[current_post_id]['additional_attr'][row['meta_key']]= phpserialize.unserialize(row['meta_value'])
File "/home/cj/.vtenv/scripts/lib/python3.5/site-packages/phpserialize.py", line 521, in loads
return load(BytesIO(data), charset, errors, decode_strings,
TypeError: a bytes-like object is required, not 'str'

最佳答案

Wordpress 将其数据存储为 PHP 序列化数据,由于 JSON,这种格式现在基本上未使用。在过去的美好时光中,它曾一度在 PHP 开发人员中非常流行(提示,wordpress 已经是古老的历史了)。

它已被废弃,因为 php 序列化和反序列化存在一些安全问题,而且还因为 JSON 的兴起。但在某些应用程序中仍然可以找到它。

使用unserialize转换为 php 对象

unserialize — Creates a PHP value from a stored representation

上面的结果会是这样的:

array(17) {
["pa_tea-coffee"]=>
array(6) {
["name"]=>
string(13) "pa_tea-coffee"
["value"]=>
string(0) ""
["position"]=>
string(1) "0"
["is_visible"]=>
int(1)
["is_variation"]=>
int(0)
["is_taxonomy"]=>
int(1)
}
["pa_bottled-mineral-water"]=>
array(6) {
["name"]=>
string(24) "pa_bottled-mineral-water"
["value"]=>
string(0) ""
["position"]=>
string(1) "1"
["is_visible"]=>
int(1)
["is_variation"]=>
int(0)
["is_taxonomy"]=>
int(1

如何将其转换为 python,这是另一个问题,因为这不是 python 原生的。但幸运的是,有这样的模块:https://pypi.python.org/pypi/phpserialize

关于python - 如何在python中解析wordpress分类数据库内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41806925/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com