我正在尝试使用python抓取网站并从中获取商品列表。我使用BeaufitulSoup解析了html,并使用json.loads(data)制作了一个JSON文件。 JSON对象如下所示:
{ ".1768j8gv7e8__0":{
"context":{
//some info
},
"pathname":"abc",
"showPhoneLoginDialog":false,
"showLoginDialog":false,
"showForgotPasswordDialog":false,
"isMobileMenuExpanded":false,
"showFbLoginEmailDialog":false,
"showRequestProductDialog":false,
"isContinueWithSite":true,
"hideCoreHeader":false,
"hideVerticalMenu":false,
"sequenceSeed":"web-157215950176521",
"theme":"default",
"offerCount":null
},
".1768j8gv7e8.6.2.0.0__6":{
"categories":[
],
"products":{
"count":12,
"items":[
{
//item info
},
{
//item info
},
{
//item info
}
],
"pageSize":50,
"nextSkip":100,
"hasMore":false
},
"featuredProductsForCategory":{
},
"currentCategory":null,
"currentManufacturer":null,
"type":"Search",
"showProductDetail":false,
"updating":false,
"notFound":false
}
}
我需要产品部分的物品清单。我该如何提取?
答案 0 :(得分:0)
只需:
const myInput = document.querySelectorAll(".move");
myInput.forEach((el) => {
el.addEventListener("keyup", function (event) {
if (event.keyCode == 37) {
console.log(event.keyCode);
if (this.parentElement.previousElementSibling != undefined) {
this.parentElement.previousElementSibling.getElementsByClassName("move")[0].focus();
}
}
else if (event.keyCode == 39) {
if (this.parentElement.nextElementSibling != undefined) {
this.parentElement.nextElementSibling.getElementsByClassName("move")[0].focus();
}
}
}, false)
})
答案 1 :(得分:0)
导入json
打包并将每个条目映射到项目列表(如果有的话):
此解决方案更通用,它将检查json中的所有项目并找到所有项目,而无需对元素的索引进行硬编码
import json
data = '{"p1": { "pathname":"abc" }, "p2": { "pathname":"abcd", "products": { "items" : [1,2,3]} }}'
# use json package to convert json string to dictionary
jsonData = json.loads(data)
type(jsonData) # dictionary
# use "list comprehension" to iterate over all the items in json file
# itemData['products']["items"] - select items from data
# if "products" in itemData.keys() - check if given item has products
[itemData['products']["items"] for itemId, itemData in jsonData.items() if "products" in itemData.keys()]
编辑:在代码中添加了注释
答案 2 :(得分:0)
我仅将您从BeautifulSoup获得的JSON文件的URL称为“ response
”,然后将其放入items
数组中的示例键,例如itemId
:>
import json
json_obj = json.load(response)
array = []
for i in json_obj['items']:
array[i] = i['itemId']
print(array)