简体   繁体   中英

Getting error while using telegram api for data scraping

MY code... I am using Python language in VS Code with Telethon library.

from telethon.sync import TelegramClient
import datetime
import pandas as pd

api_id = 18879654
api_hash = '7d78879e63e0b95612c778765e4665ba3'

chats = ['waqarzaka']


client =  TelegramClient('test', api_id, api_hash)


df = pd.DataFrame()


for chat in chats:
    with TelegramClient('test', api_id, api_hash) as client:
        for message in client.iter_messages(chat, offset_date=datetime.date.today() , reverse=True):
            print(message)
            data = { "group" : chat, "sender" : message.sender_id, "text" : message.text, "date" : message.date}

            temp_df = pd.DataFrame(data, index=[1])
            df = df.append(temp_df)

df['date'] = df['date'].dt.tz_localize(None)

df.to_excel("E:\\tel\\data_{}.xlsx".format(datetime.date.today()), index=False)

below is the result/error I used to get.

PS C:\Users\User> & C:/Users/User/AppData/Local/Programs/Python/Python311/python.exe c:/Users/User/OneDrive/Desktop/hello.py
Traceback (most recent call last):
  File "c:\Users\User\OneDrive\Desktop\hello.py", line 23, in <module>
    with TelegramClient('test', api_id, api_hash) as client:
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\User\AppData\Local\Programs\Python\Python311\Lib\site-packages\telethon\client\telegrambaseclient.py", line 294, in __init__   
    session.set_dc(
  File "C:\Users\User\AppData\Local\Programs\Python\Python311\Lib\site-packages\telethon\sessions\sqlite.py", line 168, in set_dc
    self._update_session_table()
  File "C:\Users\User\AppData\Local\Programs\Python\Python311\Lib\site-packages\telethon\sessions\sqlite.py", line 194, in _update_session_table
    c.execute('delete from sessions')
sqlite3.OperationalError: database is locked
PS C:\Users\User> 


I am trying to scrape data from telegram by using telethon(python library for telegram).

You're initialising 2 clients, no need for that. Also, you're missing the start() call on the client.

This changes should get you started

from telethon.sync import TelegramClient
import datetime
import pandas as pd

api_id = 12345678
api_hash = '7d78879e63e0b95612c778765eAAAAAAA'

chats = ['waqarzaka']


client =  TelegramClient('test', api_id, api_hash)
client.start()

df = pd.DataFrame()


for chat in chats:
    for message in client.iter_messages(chat, offset_date=datetime.date.today() , reverse=True):
        print(message)
        data = { "group" : chat, "sender" : message.sender_id, "text" : message.text, "date" : message.date}

        temp_df = pd.DataFrame(data, index=[1])
        df = df.append(temp_df)

df['date'] = df['date'].dt.tz_localize(None)

df.to_excel("E:\\tel\\data_{}.xlsx".format(datetime.date.today()), index=False)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM