簡體   English   中英

為什么 web 用 Python 抓取會給我一個錯誤?

[英]Why web Scraping with Python gives me an error?

在我的代碼中,我只想查看意大利著名商店 Unieuro 在線商店中一篇文章的獎品。 但是即使我使用 User-Agent 來允許我的連接,該程序有時也只能工作,現在我正在嘗試並且它總是給我這個錯誤:

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

如何解決這個問題? 請幫助我,這是代碼

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è')

服務器不返回頁面的原因有很多,例如,您的互聯網提供商、網站提供商、通道負載過重、臨時阻塞等。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM