【问题标题】:Trying To Get Similar Username From Six Different Files尝试从六个不同的文件中获取相似的用户名
【发布时间】:2020-06-21 14:44:01
【问题描述】:

我从 instagram 上抓取了六个不同人的关注者列表,并试图获取所有六个帐户中相同的人的用户名,但到目前为止它并不准确,因此我们将不胜感激。

这是我的代码,用于打开和读取带有关注者列表的 json 文件,并根据它们的前两个字母在字典中对它们进行排序并进行比较

import json

with open('./JSONs Old/A.json', 'r', encoding='utf-8') as f:
    A = json.load(f)
with open('./JSONs Old/B.json', 'r', encoding='utf-8') as f:
    B = json.load(f)
with open('./JSONs Old/C.json', 'r', encoding='utf-8') as f:
    C = json.load(f)
with open('./JSONs Old/D.json', 'r', encoding='utf-8') as f:
    D = json.load(f)
with open('./JSONs Old/E.json', 'r', encoding='utf-8') as f:
    E = json.load(f)
with open('./JSONs Old/F.json', 'r', encoding='utf-8') as f:
    F = json.load(f)
with open('./JSONs Old/G.json', 'r', encoding='utf-8') as f:
    G = json.load(f)

Als = {}
Bls = {}
Cls = {}
Dls = {}
Els = {}
Fls = {}
Gls = {}

# Loop For A
for each in A:
        if each['id'][:2] in Als.keys():
             Als[each['id'][:2]].append(each)
        else:
            Als[each['id'][:2]] = [each]

# Loop For B
for each in B:
        if each['id'][:2] in Bls.keys():
             Bls[each['id'][:2]].append(each)
        else:
            Bls[each['id'][:2]] = [each]

# Loop For C
for each in C:
        if each['id'][:2] in Cls.keys():
             Cls[each['id'][:2]].append(each)
        else:
            Cls[each['id'][:2]] = [each]

# Loop For D
for each in D:
        if each['id'][:2] in Dls.keys():
             Dls[each['id'][:2]].append(each)
        else:
            Dls[each['id'][:2]] = [each]

# Loop For E
for each in E:
        if each['id'][:2] in Els.keys():
             Els[each['id'][:2]].append(each)
        else:
            Els[each['id'][:2]] = [each]

# Loop For F
for each in F:
        if each['id'][:2] in Fls.keys():
             Fls[each['id'][:2]].append(each)
        else:
            Fls[each['id'][:2]] = [each]

# Loop For G
for each in G:
        if each['id'][:2] in Gls.keys():
             Gls[each['id'][:2]].append(each)
        else:
            Gls[each['id'][:2]] = [each]

matchls = []
for i in B:
    if (i['id'][:2] in Als.keys()) and (i['id'][:2] in Cls.keys()) and (i['id'][:2] in Dls.keys()) and (i['id'][:2] in Els.keys()) and (i['id'][:2] in Fls.keys()) and (i['id'][:2] in Gls.keys()):
        matchls.append(i)

print(matchls)

Json 文件包含一个人在其 Instagram 页面上拥有的所有关注者的列表,并列出容器两个键值对,如下所示

[
  {
    "name": "Name1",
    "id": "username1"
  },
  {
    "name": "Name2",
    "id": "username2"
  }
]

我想检查一个文件中的 id 是否也在其他五个文件中。 提前致谢。

【问题讨论】:

  • 你有什么问题?你的解决方案有效吗?它是如何不足的?你怀疑它的一部分吗?在针对依赖数据的代码提出问题时,重要的是在您的问题中包含数据的最小示例。您越容易让 我们 从您的问题中复制和粘贴(以便我们可以执行您的代码并测试我们的解决方案),您就越有可能获得回复。 - 请阅读minimal reproducible example

标签: python json web web-scraping


【解决方案1】:

这是一种无需排序的方法:

使用这个函数你可以查看一个follower的id是否在其他json列表中

# (param 1) follower: a single dict with including a key 'id'
# (param 2) follower_lists: list of loaded json files to check an id match
def compareFollowers(follower, follower_lists):
    for list in follower_lists: # loop through each json file
        if not any(follower['id'] == f['id'] for f in list): # check if the id is the same as another from the list
            return False # if there is no common ids, return False

    return True # if every list had a common id return True

仅供参考 any() 函数返回一个 boolean 值:

  • True 如果一个可迭代对象的至少一个元素为真
  • False 如果所有元素都为 false 或可迭代对象为空

要打印所有文件之间的所有常见关注者,您可以这样做:

import json

with open('./JSONs Old/A.json', 'r', encoding='utf-8') as f:
    A = json.load(f)
with open('./JSONs Old/B.json', 'r', encoding='utf-8') as f:
    B = json.load(f)
with open('./JSONs Old/C.json', 'r', encoding='utf-8') as f:
    C = json.load(f)
with open('./JSONs Old/D.json', 'r', encoding='utf-8') as f:
    D = json.load(f)
with open('./JSONs Old/E.json', 'r', encoding='utf-8') as f:
    E = json.load(f)
with open('./JSONs Old/F.json', 'r', encoding='utf-8') as f:
    F = json.load(f)
with open('./JSONs Old/G.json', 'r', encoding='utf-8') as f:
    G = json.load(f)


follower_lists = [B, C, D, E, F, G] # include every list but the first

for follower in A: # loop through the first list and compare each follower's id
    if compareFollowers(follower, follower_lists):
        print(follower)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-21
    • 2022-01-01
    • 2021-06-15
    • 2015-02-01
    相关资源
    最近更新 更多