gpt4 book ai didi

python - 使用Regex从Elasticsearch中的json输出中提取电子邮件地址

转载 作者:行者123 更新时间:2023-12-02 22:35:30 25 4
gpt4 key购买 nike

我的Elasticsearch索引中存储了成千上万个电报消息。我需要提取用户在Telegram上提到的电子邮件地址。电子邮件地址在[_source] [text]中,并在帖子中发布,因此我需要使用REGEX:

([\s]{0,10}[\w.]{1,63}@[\w.]{1,63}[\s]{0,10})

执行以下操作:
  • a)从每封邮件中提取电子邮件地址;
  • b)创建一个新的Maltego实体

  • 我正在尝试这段代码(对于Python /编码我是完全陌生的!),但是它不起作用:
        #!/usr/bin/env python

    from elasticsearch import Elasticsearch
    from MaltegoTransform import *
    import json
    import os
    import re


    m = MaltegoTransform()

    indexname = sys.argv[1]

    es = Elasticsearch('localhost:9200')

    res = es.search(index=indexname, size=1000, body={"query": {"match":
    {"entities.type": "email"}}})

    for doc in res['hits']['hits']:

    def get_emails(data=""):

    addresses = re.findall(r'[\s]{0,10}[\w.]{1,63}@[\w.]{1,63}[\s]{0,10}', data)
    print addresses #does not print anything#

    m.addEntity('maltego.EmailAddress', ''.join(WHAT?))

    m.returnOutput()

    这是我的json输出的示例:
        {
    took: 5,
    timed_out: false,
    _shards: {
    total: 1,
    successful: 1,
    skipped: 0,
    failed: 0
    },
    hits: {
    total: 43,
    max_score: 7.588423,
    hits: [
    {
    _index: "MY_INDEX",
    _type: "items",
    _id: "CHANNEL ID",
    _score: 7.588423,
    _source: {
    id: 2411,
    audio: { },
    author_signature: null,
    caption: null,
    channel_chat_created: null,
    chat: {},
    command: null,
    service: null,
    sticker: { },
    supergroup_chat_created: null,
    text: HERE'S THE TEXT CONTAINING EMAIL ADDRESS.

    因此,我需要搜索电子邮件的文本嵌套在[_source] [text]中。我只需要用它提取电子邮件地址(通过正则表达式),并能够打印它并在“函数”中使用它,以便在Maltego中创建图形实体。该函数如下所示:
    m.addEntity('maltego.EmailAddress', ''.join(THE EMAIL ENTITY EXTRACTED WITH REGEX)

    最佳答案

    添加电子邮件地址将取决于您的图书馆要求。正确的方法可能是对每个电子邮件地址使用addEntity()一次,或者可能是将所有地址添加到一个 call 中。

    要使用addEntity()添加每个电子邮件地址,请使用:

    es = Elasticsearch()
    res = es.search(index=indexname, size=1000, body={"query": {"match": {"entities.type": "email"}}})
    response = json.dumps(res)
    data = json.loads(response)

    fulltext = []

    for row in data['hits']['hits']:
    fulltext.append(row['_source']['text'].encode('utf8'))

    for text in fulltext:
    emails = re.findall("[\s]{0,10}([\w.]{1,63}@[\w.]{1,63})[\s]{0,10}", text)

    for email in set(emails):
    m.addEntity('maltego.EmailAddress', email)

    m.returnOutput()

    如您所见,使用 ''.join(email)将创建一个电子邮件地址之间没有定界符的字符串。要添加所有带有 ,分隔的电子邮件地址,请执行以下操作:
    emails = re.findall("[\s]{0,10}([\w.]{1,63}@[\w.]{1,63})[\s]{0,10}", text)
    m.addEntity('maltego.EmailAddress', ','.join(emails))

    关于python - 使用Regex从Elasticsearch中的json输出中提取电子邮件地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55870944/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com