Answer the question
In order to leave comments, you need to log in
Some problems with sending requests. How to decide?
I made a telegram bot that should send a request to the site and download a picture from there, and then send it to messages. I wanted to upload to free hosting https://www.pythonanywhere.com/ , but when I started I ran into the following problem:
12:54 ~/bot $ python3 bot.py
2020-09-15 12:54:43,117 (util.py:75 WorkerThread1) ERROR - TeleBot: "ProxyError occurred, args=(MaxRetryError("HTTPSConnectionPool(host='megaresheba.ru', port=443): Max retries exceeded with url: /gdz/geometriya/8-klass/merzlyak/56-nomer (Caused by ProxyError('Cannot connect to proxy.', OSError('Tunnel connection failed: 403 Forbidden')))"),)
Traceback (most recent call last):
File "/usr/lib/python3.8/site-packages/urllib3/connectionpool.py", line 594, in urlopen
self._prepare_proxy(conn)
File "/usr/lib/python3.8/site-packages/urllib3/connectionpool.py", line 805, in _prepare_proxy
conn.connect()
File "/usr/lib/python3.8/site-packages/urllib3/connection.py", line 308, in connect
self._tunnel()
File "/usr/lib/python3.8/http/client.py", line 898, in _tunnel
raise OSError("Tunnel connection failed: %d %s" % (code,
OSError: Tunnel connection failed: 403 Forbidden
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/lib/python3.8/site-packages/requests/adapters.py", line 439, in send
resp = conn.urlopen(
File "/usr/lib/python3.8/site-packages/urllib3/connectionpool.py", line 637, in urlopen
retries = retries.increment(method, url, error=e, _pool=self,
File "/usr/lib/python3.8/site-packages/urllib3/util/retry.py", line 399, in increment
raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='megaresheba.ru', port=443): Max retries exceeded with url: /gdz/geometriya/8-klass/merzlyak/56-nomer (Caused by ProxyError('Cannot connect to proxy.', OSError('Tunnel connection failed: 403 Forbidden')))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/vo2va2/.local/lib/python3.8/site-packages/telebot/util.py", line 69, in run
task(*args, **kwargs)
File "bot.py", line 372, in geom
parse()
File "bot.py", line 368, in parse
html = get_html(URL)
File "bot.py", line 344, in get_html
r = requests.get(url, headers=HEADERS, params=params)
File "/usr/lib/python3.8/site-packages/requests/api.py", line 75, in get
return request('get', url, params=params, **kwargs)
File "/usr/lib/python3.8/site-packages/requests/api.py", line 60, in request
return session.request(method=method, url=url, **kwargs)
File "/usr/lib/python3.8/site-packages/requests/sessions.py", line 533, in request
resp = self.send(prep, **send_kwargs)
File "/usr/lib/python3.8/site-packages/requests/sessions.py", line 646, in send
r = adapter.send(request, **kwargs)
File "/usr/lib/python3.8/site-packages/requests/adapters.py", line 510, in send
raise ProxyError(e, request=request)
requests.exceptions.ProxyError: HTTPSConnectionPool(host='megaresheba.ru', port=443): Max retries exceeded with url: /gdz/geometriya/8-klass/merzlyak/56-nomer (Caused by ProxyError('Cannot connect to proxy.', OSError('Tunnel connection failed: 403 Forbidden')))
"
"
Traceback (most recent call last):
File "/usr/lib/python3.8/site-packages/urllib3/connectionpool.py", line 594, in urlopen
self._prepare_proxy(conn)
File "/usr/lib/python3.8/site-packages/urllib3/connectionpool.py", line 805, in _prepare_proxy
conn.connect()
File "/usr/lib/python3.8/site-packages/urllib3/connection.py", line 308, in connect
self._tunnel()
File "/usr/lib/python3.8/http/client.py", line 898, in _tunnel
raise OSError("Tunnel connection failed: %d %s" % (code,
OSError: Tunnel connection failed: 403 Forbidden
During handling of the above exception, another exception occurred:
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "bot.py", line 397, in <module>
bot.polling(none_stop=True)
File "/home/vo2va2/.local/lib/python3.8/site-packages/telebot/__init__.py", line 427, in polling
self.__threaded_polling(none_stop, interval, timeout)
File "/home/vo2va2/.local/lib/python3.8/site-packages/telebot/__init__.py", line 451, in __threaded_polling
self.worker_pool.raise_exceptions()
File "/home/vo2va2/.local/lib/python3.8/site-packages/telebot/util.py", line 118, in raise_exceptions
six.reraise(self.exc_info[0], self.exc_info[1], self.exc_info[2])
File "/usr/lib/python3.8/site-packages/six.py", line 696, in reraise
raise value
File "/home/vo2va2/.local/lib/python3.8/site-packages/telebot/util.py", line 69, in run
task(*args, **kwargs)
File "bot.py", line 372, in geom
parse()
File "bot.py", line 368, in parse
html = get_html(URL)
File "bot.py", line 344, in get_html
r = requests.get(url, headers=HEADERS, params=params)
File "/usr/lib/python3.8/site-packages/requests/api.py", line 75, in get
return request('get', url, params=params, **kwargs)
File "/usr/lib/python3.8/site-packages/requests/api.py", line 60, in request
return session.request(method=method, url=url, **kwargs)
File "/usr/lib/python3.8/site-packages/requests/sessions.py", line 533, in request
resp = self.send(prep, **send_kwargs)
File "/usr/lib/python3.8/site-packages/requests/sessions.py", line 646, in send
r = adapter.send(request, **kwargs)
File "/usr/lib/python3.8/site-packages/requests/adapters.py", line 510, in send
raise ProxyError(e, request=request)
requests.exceptions.ProxyError: HTTPSConnectionPool(host='megaresheba.ru', port=443): Max retries exceeded with url: /gdz/geometriya/8-klass/merzlyak/56-nomer (Caused by ProxyError('Cannot connect to proxy.', OSError('Tunnel connection failed: 403 Forbidden')))
import telebot
from telebot import types
import requests
from bs4 import BeautifulSoup
import os
try:
import Image
except ImportError:
from PIL import Image
bot = telebot.TeleBot(token)
@bot.message_handler(commands= ['start'])
def first(message):
key = types.ReplyKeyboardMarkup(True, False)
key.row('Расписание', 'ГДЗ')
send = bot.send_message(message.from_user.id, "Меню", reply_markup=key)
bot.register_next_step_handler(send, menu)
def menu(message):
if message.text == 'Расписание':
img = open('/home/vo2va2/bot/2020.09.04-18.08.png', 'rb')
bot.send_photo(message.chat.id, photo=img)
keyboard = types.ReplyKeyboardMarkup(resize_keyboard=True, one_time_keyboard=False)
keyboard.add(*[types.KeyboardButton(name) for name in ['Домой']])
send = bot.send_message(message.from_user.id, "2 lvl", reply_markup=keyboard)
bot.register_next_step_handler(send, subject)
elif message.text == 'ГДЗ':
keyboard = types.ReplyKeyboardMarkup(resize_keyboard=True)
keyboard.add(*[types.KeyboardButton(name) for name in ['Английский язык', 'Алгебра']])
keyboard.add(*[types.KeyboardButton(name) for name in ['Геометрия']])
#keyboard.add(*[types.KeyboardButton(name) for name in ['нет', 'да', '']])
#keyboard.add(*[types.KeyboardButton(name) for name in ['нет', 'да', '']])
keyboard.add(*[types.KeyboardButton(name) for name in ['Домой']])
send = bot.send_message(message.from_user.id, "2 уровень", reply_markup=keyboard)
bot.register_next_step_handler(send, subject)
def subject(message):
elif message.text == 'Геометрия':
keyboard = types.ReplyKeyboardMarkup(resize_keyboard=True, one_time_keyboard=False)
send = bot.send_message(message.from_user.id, "Введите номер упражнения", reply_markup=keyboard)
bot.register_next_step_handler(send, geom)
def geom(message):
if message.text != 'Назад':
intmes = int(message.text)
listnum = range(1, 861)
if intmes in listnum:
per = str(intmes)
URL = 'https://megaresheba.ru/gdz/geometriya/8-klass/merzlyak/' + per + '-nomer'
HEADERS = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36',
'accept': '*/*'}
def get_html(url, params=None):
r = requests.get(url, headers=HEADERS, params=params)
return r
imgg = ''
def get_content(html):
global imgg
soup = BeautifulSoup(html, 'html.parser')
div = soup.find('div', {'class': 'with-overtask'})
# if div != None:
imgg = div.find('img')['src']
p = requests.get(imgg) #download
out = open(r"C:\Users\vayak\PycharmProjects\pythonProject5\img.jpg", "wb")
out.write(p.content)
out.close()
img = open('img.jpg', 'rb')
bot.send_photo(message.chat.id, photo=img)
img.close()
file_path = r'C:\Users\vayak\PycharmProjects\pythonProject5\img.jpg'
os.remove(file_path) #delete
send = bot.send_message(message.from_user.id, "Решение упражнения " + per)
bot.register_next_step_handler(send, back) #recurse
def parse():
html = get_html(URL)
if html.status_code == 200:
get_content(html.text)
return html
parse()
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question