在爬取JSON文件过程中,经常遇到字典,而我们需要爬取的关键信息就隐藏在字典里面。
如何高效地、准确无误地把字典中的关键信息给提取出来?
个人感觉,由于字典是没有索引的,不能像列表一样根据索引号一个个展现。
如果不知道字典的键,是无法将字典的值给提取出来的。
下面这个方法挺有用的:
payload = {"contentType":"json",
"head":{"cid":"09031046415366971248","ctok":"","cver":"1.0","lang":"01","sid":"8888","syscode":"09","auth":"","extension":[]},
"client":{"version":"","channel":114,"locale":"zh-CN","currency":"CNY","source":"NVacationSearchV2","cid":"1697761180669.44uqrm","location":{"lat":"","lon":"","cityId":299,"cityType":3,"locatedCityId":0},"extras":{}},
"marketingInfo":{"allianceId":1315,"sid":1535},
"destination":{"poid":0,"type":"","keyword":"惠州"},
"filtered":{"tab":"126","preItems":[],"items":[],"minPrice":0,"maxPrice":0,"beginDate":"","endDate":"","sort":8,"pageIndex":'1',"pageSize":30,"extras":{}},
"searchOption":{"returnMode":"all","filters":["ProductNewLine,ProductLine,PLAY_LINE_EX,HotDestination,HotScenicSpot,SaleDepartureStat,TravelDays,DepartureDate,ProductPattern,ProductLevel,ADSuitPersons,ADServiceCharacteristic,ADIsDirectFlight,ADHotelCharacteristic,ADNLineCharacteristic,ProductDistrict,ProviderBrand,PriceRange,ADServiceGarantee,PRESALE_STATUS"],"needAdProduct":False,"needUpStream":False,"needRiskPolicyInfo":True},
"imageOption":{"width":420,"height":420,"autoCrop":True},"productOption":{"needBasicInfo":True,"needPrice":True,"needVendor":True,"needComment":True,"needOrder":True,"needRanking":True,"tagOption":["PRODUCT_TAG","RECOMMEND_TAG","FESTIVAL_TAG","SCHEDULE_TAG","PROMOTION_TAG","BARGAIN_TAG","FAVORITE_TAG"]},
"productKeys":[],
"requestSource":"tour",
"debug":False,
"extras":{"USE_OLD_RISK":"True","USE_OLD_FILTER":"true","USE_NEW_LEVEL":"true","USE_NEW_PRICE":"true","needUserDiscountPrice":"true","FILTERED_SCOPE":"custom","NEED_DEPARTURE_TAB_REDIRECT":"true","NEED_PRE_SALE_FILTER":"true","DEFAULT_NATIVE_REQUEST":"true","TAB_SCENE_CODE":"pc_online"}
}
keys_payload = payload.keys()
for key in keys_payload:
print(key)
print(len(keys_payload))
终端输出结果为:
这样,我们就可以很方便地知道爬取回来的字典包含有哪些键了。
如果键所包含的值,仍然是字典,可以再用一次上述方法查看内容,直到最终找到自己想要的关键信息为止!这对于小白分析复杂的JSON文件有很大的帮助,希望对你有用。
好了,爬虫小知识就分享到此了~