添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

Scrapy。在Python上将Javascript数组转换为Json

1 人关注

我一直在为我正在使用scrapy制作的一个网站苦恼。 这个网站用产品数据返回一系列的Javascript变量(数组)。

datos[0] = ["12345","3M YELLOW CAT5E CABLE","6.81","1","A","N","N","N","N","N",0,0,0,0,0,"0","0","0","0","0","P","001-0030","12","40K8957","28396","250","Due: 30-12-1899",0.0000,1,"",\'\'];
datos[1] = ["12346","3M GREEN CAT5E CABLE","7.81","1","A","N","N","N","N","N",0,0,0,0,0,"0","0","0","0","0","P","001-0030","12","40K8957","28396","250","Due: 30-12-1899",0.0000,1,"",\'\'];

So on...

用scrapy将数组取为字符串是很容易的,因为网站的响应会打印出这些变量。 问题是我想把它转化为Json,这样我就可以处理它并把它存储在数据库表中。

通常我会用Javascript的函数Json.stringify将其转换为Json并在PHP中发布。

然而,当使用Python的json.load,甚至StringIO时,我无法将数组加载到json。

可能是一个格式错误,但我无法识别它,因为我不是Json或Python专家。

EDIT: 我刚刚意识到,既然Scrapy不能执行Javascript,那么主要问题是数据只是一个字符串。我应该把它格式化为Json格式。

我们非常欢迎任何帮助。

1 个评论
你能检查一下下面的答案是否对你有用吗?如果可以的话,请将其标记为已接受,我将不胜感激。谢谢!
javascript
python
json
scrapy
ofesad
ofesad
发布于 2022-06-18
2 个回答
Max
Max
发布于 2022-06-18
已采纳
0 人赞同

如果你想取一个数组并创建一个json对象,你可以这样做。

values = ["12345","3M YELLOW CAT5E CABLE","6.81","1","A","N","N","N","N","N",0,0,0,0,0,"0","0","0","0","0","P","001-0030","12","40K8957","28396","250","Due: 30-12-1899",0.0000,1]
keys = [x for x in range(len(values))]
d = dict(zip(keys, values))