info_eles = driver.find_element_by_class_name('Ptable-item')
result_list = []
for info_ele in info_eles:
info_ele_dict = get_info_ele_dict(info_ele)
result_list.append(info_ele_dict)
save_goods_info(result_list)
def get_info_ele_dict(info_ele):
compter_part = info_ele.find_element_by_tag_name("h3")
#拿到key值
compter_info_keys = info_ele.find_element_by_tag_name("dt")
#拿到value值
compter_info_values = info_ele.find_elements_by_xpath("dl//dd[not(contant(@class,'Ptable-tips'))]")
#存储信息的key值和value值
key_and_value_dict = {}
#存储计算机所有组成信息
parts_dict = {}
#解析
for i in range(len(compter_info_keys)):
key_and_value_dict[compter_info_keys[i].text]=compter_info_values[i].text
parts_dict[compter_part.text] = key_and_value_dict
return parts_dict
def save_goods_info(info_list):
project_path = os.path.dirname(os.getcwd())
file_path = project_path + "goods_infos"
if not os.path.exists(file_path):
os.mkdir(file_path)
else:
return file_path
with open(file_path+"computer.infos","a",encoding="utf-8") as f:
f.write(str(info_list))
print(str(info_list))