Telethon 上周仅从电报组中抓取

问题描述

我有一个用于抓取的电视广播脚本。 我对这个脚本有几个问题:

  1. 此脚本抓取本月在线的用户。 应该怎么做才能使它只在最后 7 天刮擦?还是更多……?

  2. 此脚本将用户写入 csv 文件。 如何使其写入 xlsx 文件

  3. 当脚本完成它的工作时,它会以以下格式写入“last seen”: "UserStatusOffline(was_online=datetime.datetime(2021,4,16,10,52,53,tzinfo=datetime.timezone.utc))" 如何使它以这种格式写入: was_online = (年、月、日)?

或者不同的东西... 保持简短很重要。

谢谢大家,祝你有美好的一天! 这是脚本:

from telethon.sync import TelegramClient
from telethon.tl.functions.messages import GetDialogsRequest
from telethon.tl.types import InputPeerEmpty
from telethon.sync import TelegramClient
from telethon.tl.functions.messages import GetDialogsRequest
from telethon.tl.types import InputPeerEmpty,InputPeerChannel,InputPeerUser
from telethon.errors.rpcerrorlist import PeerFloodError,UserPrivacyRestrictedError
from telethon.tl.functions.channels import InvitetochannelRequest
import sys
import csv
import traceback
import time
from datetime import datetime
api_id = 39xxx18
api_hash = '70dddb7634xxxxxxxxxf3bab4b030738'
phone = 'xxxxxxxxxx'
client = TelegramClient(phone,api_id,api_hash)

client.connect()
if not client.is_user_authorized():
    client.send_code_request(phone)
    client.sign_in(phone,input('Enter the code recieved to your Telegram messenger: '))


chats = []
last_date = None
chunk_size = 200
groups=[]

result = client(GetDialogsRequest(
             offset_date=last_date,offset_id=0,offset_peer=InputPeerEmpty(),limit=chunk_size,hash = 0
         ))
chats.extend(result.chats)

for chat in chats:
    try:
        if chat.megagroup== True:
            groups.append(chat)
    except:
        continue

print('Choose a group to scrape members from:')
i=0
for g in groups:
    print(str(i) + '- ' + g.title)
    i+=1

g_index = input("Enter a Number: ")
target_group=groups[int(g_index)]

print('Fetching Members...')
all_participants = []
all_participants = client.get_participants(target_group,aggressive=True)

print('Saving In file...')
with open("members.csv","w",encoding='UTF-8') as f:
    writer = csv.writer(f,delimiter=",",lineterminator="\n")
    writer.writerow(['username','last seen'])
    for user in all_participants:
        accept=True
        try:
            lastDate=user.status.was_online
            num_months = (datetime.Now().year - lastDate.year) * 12 + (datetime.Now().month - lastDate.month)
            if(num_months>0.5):
                accept=False
        except:
            continue

        if (accept) :
            if user.username:
                username= user.username
            else:
                username= ""
            if user.first_name:
                first_name= user.first_name
            else:
                first_name= ""
            if user.last_name:
                last_name= user.last_name
            else:
                last_name= ""
            name= (first_name + ' ' + last_name).strip()
            writer.writerow([username,user.status]) 

print('Members scraped successfully.')

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)