Как исправить вечную ошибку в боте, который использует парсинг?
import requests
from bs4 import BeautifulSoup
import time
import threading
from collections import defaultdict
import logging
import vk_api
from vk_api.bot_longpoll import VkBotEventType, VkBotLongPoll
# Настройка логирования
logging.basicConfig(level=logging.INFO)
def write_msg(peer_id, message):
vk.method('messages.send', {'peer_id': peer_id, 'message': message, 'random_id': 0})
key = 'vk1.a.0vmA-PHEPh8IMRca6h7gI_CK28sNMcWlbNE3bVJp4lyc-LOQdpcVEvb6c5w2aU8AxXyGc9Vp7WQ_3ruiFaD9xurIK2hSGkiB32Tm44uaO5b8GxfHSxnoMnmvc2lVvwpCywaDb6iW0dfwL7Sf8aWNqtk4_ZqYyjFfAm9s1mn9kypf-MinNcgDpGErPIoYfX-qRmowz1X2t2LiC2Lyhqx9tg'
url = 'https://forum.mordor-rp.com/index.php?reports/'
url_1 = 'https://forum.mordor-rp.com/index.php?reports/closed'
url_2 = 'https://forum.mordor-rp.com/index.php?forums/Общая-курилка.560/'
url_3 = 'https://forum.mordor-rp.com/index.php?forums/Транспорт.178/'
url_4 = 'https://forum.mordor-rp.com/index.php?forums/Недвижимость.179/'
url_5 = 'https://forum.mordor-rp.com/index.php?forums/Предприятия.180/'
url_6 = 'https://forum.mordor-rp.com/index.php?forums/Аксессуары-Одежда-Материалы.320/'
url_7 = 'https://forum.mordor-rp.com/index.php?whats-new/latest-activity'
last = ['лох', 'шлюха', 'мать', 'пидорас']
user_agents = ("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 "
"Safari/537.36 OPR/105.0.0.0")
vk = vk_api.VkApi(token=key)
longpoll = VkBotLongPoll(vk, 226970140)
def load_cookies(file_path):
cookies = {}
with open(file_path, 'r') as file:
for line in file:
if '#' in line:
line = line.split('#')[0].strip()
if line:
parts = line.split('\t')
if len(parts) >= 7:
cookies[parts[5]] = parts[6].strip() # Удаляем символы новой строки
return cookies
cookie = load_cookies('cookies.txt')
# #curl.exe -c cookies.txt -b cookies.txt -L -d "username=&password=" https://forum.mordor-rp.com/index.php?login/
cookies = {
'xf_csrf': '',
'xf_session': ''
}
def get_message():
response = requests.get(url=url_7, cookies=cookies)
soup = BeautifulSoup(response.text, 'html.parser')
link = soup.find('div', class_='contentRow-title')
full_link = link.find_all('a')[0].find_next('a').get('href')
data = soup.find('div', class_='contentRow-snippet').text
for item in last:
if item in data:
return item, full_link
return None, None
def get_aks():
response = requests.get(url=url_6, cookies=cookies)
soup = BeautifulSoup(response.text, 'html.parser')
data = soup.find('div', class_='structItemContainer-group js-threadList')
if data is None:
data = ""
print(data)
return 1, 1
else:
first = data.find('div', class_='structItem-title')
l = first.find_all('a')[0].find_next('a').get('href')
answers = data.find('dd').text
return l, answers
def get_pred():
response = requests.get(url=url_5, cookies=cookies)
soup = BeautifulSoup(response.text, 'html.parser')
data = soup.find('div', class_='structItemContainer-group js-threadList')
if data is None:
data = ""
print(data)
return 1, 1
else:
first = data.find('div', class_='structItem-title')
l = first.find_all('a')[0].find_next('a').get('href')
answers = data.find('dd').text
return l, answers
def get_ned():
response = requests.get(url=url_4, cookies=cookies)
soup = BeautifulSoup(response.text, 'html.parser')
data = soup.find('div', class_='structItemContainer-group js-threadList')
if data is None:
data = ""
print(data)
return 1, 1
else:
first = data.find('div', class_='structItem-title')
l = first.find_all('a')[0].find_next('a').get('href')
answers = data.find('dd').text
return l, answers
def get_transport():
response = requests.get(url=url_3, cookies=cookies)
soup = BeautifulSoup(response.text, 'html.parser')
data = soup.find('div', class_='structItemContainer-group js-threadList')
if data is None:
data = ""
print(data)
return 1, 1
else:
first = data.find('div', class_='structItem-title')
l = first.find_all('a')[0].find_next('a').get('href')
answers = data.find('dd').text
return l, answers
def get_smoking():
response = requests.get(url=url_2, cookies=cookies)
soup = BeautifulSoup(response.text, 'html.parser')
data = soup.find('div', class_='structItemContainer-group js-threadList')
nick1 = data.find('ul', class_='structItem-parts')
nick = nick1.find('a', class_='username').text
title = data.find('div', class_='structItem-title').text
link = data.find('div', class_='structItem-title')
link = link.find('a').get('href')
answers = data.find('dd').text
return nick, title, link, answers
def get_week():
response = requests.get(url_1, cookies=cookies)
soup = BeautifulSoup(response.text, 'html.parser')
name_counts = defaultdict(int)
data = soup.find_all('div', class_='structItem structItem--report')
for item in data:
step = item.find('div', class_='structItem-cell structItem-cell--latest')
step1 = step.find('div', class_='structItem-minor').text.strip()
name_counts[step1] += 1
sorted_names = sorted(name_counts.items(), key=lambda x: x[1], reverse=True)
return sorted_names
def get_status():
response = requests.get(url, cookies=cookies)
soup = BeautifulSoup(response.text, 'html.parser')
data = soup.find('div', class_='p-pageWrapper')
ll = data.find('div', class_='xb-page-wrapper')
two = ll.find('div', class_='structItem-cell structItem-cell--main')
status = two.find('li').text
return status.strip()
def get_report_data():
response = requests.get(url, cookies=cookies)
soup = BeautifulSoup(response.text, 'html.parser')
data = soup.find('div', class_='p-pageWrapper')
line = soup.find_all('div', class_='structItem-cell structItem-cell--latest')
# Getting the number of reports
one = data.find('a').get('data-badge')
if one is None:
one = 0 # Handle the case where there are no reports
ll = data.find('div', class_='xb-page-wrapper')
two = ll.find('div', class_='structItem-cell structItem-cell--main')
link = two.find('a').get('href')
full_link = 'https://forum.mordor-rp.com' + link
# Extracting href from each element in line
hrefs = []
for item in line:
a_tag = item.find('a')
if a_tag:
href = a_tag.get('href')
if href:
full_href = 'https://forum.mordor-rp.com' + href
hrefs.append(full_href)
return one, full_link, hrefs
def check_for_new_words(last_link):
message, link = get_message()
if link != last_link:
write_msg(2000000004, f'?Возможно найдено нарушение!\n'
f'Возможное нарушение: "{message}"\n'
f'Ссылка на нарушение: https://forum.mordor-rp.com{link}')
return link
return last_link
def check_for_new_reports(last_link):
one, link, hrefs = get_report_data()
status = get_status()
if link != last_link and status == 'Открыто':
if int(one) >= 5:
for i, href in enumerate(hrefs, start=1):
if i <= int(one):
write_msg(2000000006, f' @all Уже {one} жалоб!\n'
f'{i}.{href}')
continue
elif int(one) < 5:
write_msg(2000000006, f'@online Пришла новая жалоба: {link}')
return link
return last_link
def check_aks(last_link):
link, answers = get_aks()
if link != last_link and int(answers) == 0:
write_msg(2000000005, f'@online Создана новая тема в Аксессуарах/Одежде/Материалах:\n'
f'Ссылка на тему: https://forum.mordor-rp.com{link[0:-5]}')
return link
return last_link
def check_ned(last_link):
link, answers = get_ned()
if link != last_link and int(answers) == 0:
write_msg(2000000005, f'@online Создана новая тема в Недвижимости:\n'
f'Ссылка на тему: https://forum.mordor-rp.com{link[0:-5]}')
return link
return last_link
def check_ts(last_link):
link, answers = get_transport()
if link != last_link and int(answers) == 0:
write_msg(2000000005, f'@online Создана новая тема в Транспорте:\n'
f'Ссылка на тему: https://forum.mordor-rp.com{link[0:-5]}')
return link
return last_link
def check_pred(last_link):
link, answers = get_pred()
if link != last_link and int(answers) == 0:
write_msg(2000000005, f'@online Создана новая тема в Предприятиях:\n'
f'Ссылка на тему: https://forum.mordor-rp.com{link[0:-5]}')
return link
return last_link
def check_for_new_titles(last_link):
nick, title, link, answers = get_smoking()
if link != last_link and int(answers) == 0:
write_msg(2000000005, '@online Создана новая тема в Курилке!\n'
f'Название темы: {title}\n'
f'Ник-нейм создателя темы:{nick}\n'
f'Ссылка на тему: https://forum.mordor-rp.com{link}\n')
return link
return last_link
def listen_for_messages():
for event in longpoll.listen():
if event.type == VkBotEventType.MESSAGE_NEW:
request = event.object.message['text']
peer_id = event.object.message['peer_id']
logging.info(f"Received message: {request} from peer_id: {peer_id}")
if request.lower() == 'жалобы':
one, link, _ = get_report_data()
status = get_status()
write_msg(peer_id, f'На данный момент количество жалоб: {one}\nСсылка на последнюю жб: {link}\n'
f'Статус жалобы: {status}')
elif request.lower() == 'send':
write_msg(2000000005, 'А вы пожелали сладких снов всем своим близким?\n'
'Напишите ответ: Да/Нет')
elif request.lower() == 'отчеты':
week = get_week()
text = ('Отчеты недели\n'
'Владимир Шевцов - топчик\n')
for name, count in week:
text += f'{name}: {count}\n'
write_msg(peer_id, text)
elif request.lower() == 'курилка':
nick, title, link, answers = get_smoking()
write_msg(peer_id, f'Название темы: {title}\n'
f'Ник-нейм создателя темы:{nick}\n'
f'Ссылка на тему: https://forum.mordor-rp.com{link}\n'
f'Количество ответов в теме: {answers}')
elif request.lower() == 'пп':
l, _ = get_transport()
k, _ = get_ned()
m, _ = get_pred()
n, _ = get_aks()
write_msg(peer_id, f'Первая разделе транспорт:\n https://forum.mordor-rp.com{l}\n'
f'Первая разделе недвижимость: https://forum.mordor-rp.com{k}\n'
f'Первая разделе предприятия: https://forum.mordor-rp.com{m}\n'
f'Первая разделе аксессуары/одежда/материалы: https://forum.mordor-rp.com{n}')
elif request.lower() == 'чек':
mes, w = get_message()
if mes is not None:
write_msg(peer_id, f'?Возможно найдено нарушение!\n'
f'Возможное нарушение: "{mes}"\n'
f'Ссылка на нарушение: https://forum.mordor-rp.com{w}')
else:
write_msg(peer_id, 'Ничего не найдено')
elif event.type == VkBotEventType.GROUP_JOIN:
peer_id = event.object.message['peer_id']
write_msg(peer_id, 'Хай')
message_thread = threading.Thread(target=listen_for_messages)
message_thread.start()
last_link_aks = ""
last_link_ned = ""
last_link_ts = ""
last_link_pred = ""
last_link_reports = ""
last_link_titles = ""
last_link_mess = ""
while True:
last_link_aks = check_aks(last_link_aks)
last_link_ned = check_ned(last_link_ned)
last_link_ts = check_ts(last_link_ts)
last_link_pred = check_pred(last_link_pred)
last_link_reports = check_for_new_reports(last_link_reports)
last_link_titles = check_for_new_titles(last_link_titles)
last_link_mess = check_for_new_words(last_link_mess)
time.sleep(1)
Сразу говорю, да использовал GPT, но я пока не нашел выхода
line 179, in get_report_data
two = ll.find('div', class_='structItem-cell structItem-cell--main')
^^^^^^^
AttributeError: 'NoneType' object has no attribute 'find'