关于neo4j作业

Excel数据表部分,找不到号可以问

from jsonpath import jsonpath
import pandas as pd
import requests
import json
from fake_useragent import UserAgent
base_url='https://www.ihchina.cn/art/representative.html?province=460000&rx_time=&type=&sex=&keywords=&limit=10&p={}'
            #换“province=”后的数字为自己省
            #北京110000  #天津120000  #河北130000  #山西140000 
data=[]
i=1
while i<=2:
    print("第"+str(i)+"页")
    headers={
        'User-Agent':UserAgent().chrome
    }
    url=base_url.format(i)
    reps=requests.get(url,headers=headers)
    info=reps.text
    # print(info)
    pnames=jsonpath(json.loads(reps.text),'$..title')
    fnames=jsonpath(reps.json(),'$..project')
    ftypes=jsonpath(reps.json(),'$..type')
    genders=jsonpath(reps.json(),'$..sex')
    nations=jsonpath(reps.json(),'$..nation')
    orders=jsonpath(reps.json(),'$..project_num')
    dricts=jsonpath(reps.json(),'$..province')
    for pname, fname, ftype, gender, nation, order, drict in zip(pnames, fnames, ftypes, genders, nations, orders,
                                                                 dricts):
        data.append([pname, fname, ftype, gender, nation, order, drict])

    # 创建DataFrame
    df = pd.DataFrame(data, columns=['传承人', '非遗', '类型', '性别', '民族', '编号', '地区'])

    # 将DataFrame写入Excel文件
    df.to_excel('海南.xlsx', index=False)
    i+=1

neo4j配置无误后操作以下部分

from py2neo import Graph, Node, Relationship
import pandas as pd


# Neo4j数据库连接信息
uri = "neo4j://localhost:7687"
username = "neo4j"#你的用户名
password = "密码"#你的密码
# 连接到数据库
graph = Graph(uri, auth=(username, password))

# 读取 Excel 表格
excel_file = "E:\Spider\Test\非遗1.xlsx"  #你的 Excel 文件路径
df = pd.read_excel(excel_file)

# 添加陕西省节点
shaanxi_node = Node("省份", name="陕西大全")

# 将陕西省节点添加到图数据库中
graph.merge(shaanxi_node, "Location", "name")
# 添加数据到数据库
for index, row in df.iterrows():
    inheritor = row['传承人']
    heritage = row['非遗']
    heritage_type = row['类型']
    gender = row['性别']
    ethnicity = row['民族']
    batch = row['编号']
    location = row['地区']

    # 创建节点
    inheritor_node = Node("Inheritor", name=inheritor, gender=gender, ethnicity=ethnicity)
    heritage_node = Node("Heritage", name=heritage, type=heritage_type,batch=batch)
    location_node = Node("Location", name=location)

    # 创建关系
    inheritor_relationship = Relationship(heritage_node, "有传承人",inheritor_node)
    location_relationship = Relationship(heritage_node, "位于", location_node)
    shaanxi_relationship = Relationship(shaanxi_node, "包含", location_node)

    # 将节点和关系添加到图数据库中
    graph.merge(inheritor_node, "Inheritor", "name")
    graph.merge(heritage_node, "Heritage", "name")
    graph.merge(location_node, "Location", "name")
    graph.merge(inheritor_relationship)
    graph.merge(location_relationship)
    graph.merge(shaanxi_relationship)

结果大概如下可根据自己需求微调第二段代码

  • 5
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值