Why web Scraping with Python gives me an error?

Viewed 31

In my code i simply want to check the prize of one article in Unieuro online store, a famous store in italy. But even if i used the User-Agent for allow my connection the program works only sometimes, right now i'm trying and it gives me always this error:

requests.exceptions.ConnectionError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))

How to solve this? Please help me, here is the code

from bs4 import BeautifulSoup
import requests
try:
    url = 'https://www.unieuro.it/online/Giochi-Playstation-5/The-Last-of-Us-Parte-I-pidSON9405597' #Unieuro
    headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36'}
    pagina = requests.get(url,headers=headers)
    soup = BeautifulSoup(pagina.content,'html.parser')
    prezzo = soup.find(id='features').get_text()
    prezzo = prezzo.split('Iva Inclusa')[0]
    if prezzo.count('%'):
        prezzo = prezzo.split('%')[-1]
        prezzo = prezzo.strip()
        prezzo = prezzo[0:7]
        print(prezzo)   #fine the last Unieuro
        # manda email del prezzo scontato
    else:
        print('Prezzo non scontato fratellì')
        # manda email prezzo non scontato (80,99 €) Unieuro
except requests.exceptions.ConnectionError:
    print('Errore fratè')
1 Answers

The fact that the server does not return the page can be due to a lot of things, for example, your Internet provider, website provider, heavy channel load, temporary blocking, etc.

import requests
from bs4 import BeautifulSoup


url = "https://www.unieuro.it/online/Giochi-Playstation-5/The-Last-of-Us-Parte-I-pidSON9405597"
response = requests.get(url)
soup = BeautifulSoup(response.text, 'lxml')
current_price = soup.find('div', class_='pdp-right__price').get_text(strip=True)
original_price = soup.find('span', class_='original-price').get_text(strip=True)
print(current_price, '\u0336'.join(original_price))

OUTPUT:

€64,00 €̶8̶0̶,̶9̶9
Related