首页 > 解决方案 > 从 YouTube 构建此亚马逊价格跟踪器:socket.gaierror: [Errno 11001] getaddrinfo failed

问题描述

我正在关注关于从 YouTube 构建这个 Amazon Price Tracker 的 DevEd 教程。我得到了他没有得到的这个错误。顺便说一句,我是 Python 的初学者。我认为问题出在发送电子邮件部分。这是代码:

import requests
from bs4 import BeautifulSoup
import smtplib
import socket

URL = "https://www.amazon.in/gp/product/B07XFXBCDD/ref=s9_acss_bw_cg_Consoles_2b1_w?pf_rd_m=A1K21FY43GMZF8&pf_rd_s=merchandised-search-3&pf_rd_r=EGQ1AY1Z7KG6N88S04CZ&pf_rd_t=101&pf_rd_p=567ef75a-71d3-443f-81cc-7dbb05bcb6c3&pf_rd_i=4092115031"

header = {"User-Agent" : "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Safari/537.36"}


def check_price(): #checks the price on amazon and calls the send_mail() function if condition is true

    page = requests.get(URL.strip('\n'), headers=header)

    soup = BeautifulSoup(page.content, 'html.parser')

    price = soup.find(id='priceblock_ourprice').get_text()

    price = price.replace(',','')
    converted_price = float(price[3:9])

    if converted_price > 28000:
        send_mail()


def send_mail():

    server = smtplib.SMTP('smtp.google.com',587)
    server.ehlo()
    server.starttls()
    server.ehlo()
    server.login('officialyashuppal@gmail.com', <password>)

    subject= 'Price fell down'
    body= 'Check link ' + URL

    msg=f"Subject: {subject} \n\n {body}"

    server.sendmail(
        'officialyashuppal@gmail.com',
        'officialyashuppal@gmail.com',
        msg
    )
    print('email sent')

    server.quit()

check_price()

这是错误:

回溯(最近一次通话最后):

File "c:/Users/Yash/Desktop/Projects/PriceTracker/price.py", line 49, in <module>
    check_price()
  File "c:/Users/Yash/Desktop/Projects/PriceTracker/price.py", line 24, in check_price
    send_mail()
  File "c:/Users/Yash/Desktop/Projects/PriceTracker/price.py", line 29, in send_mail
    server = smtplib.SMTP('smtp.google.com',587)
  File "C:\ProgramData\Anaconda3\lib\smtplib.py", line 251, in __init__
    (code, msg) = self.connect(host, port)
  File "C:\ProgramData\Anaconda3\lib\smtplib.py", line 336, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "C:\ProgramData\Anaconda3\lib\smtplib.py", line 307, in _get_socket
    self.source_address)
  File "C:\ProgramData\Anaconda3\lib\socket.py", line 707, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
  File "C:\ProgramData\Anaconda3\lib\socket.py", line 748, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed

标签: pythonamazon

解决方案


那是因为smtp.google.com没有解决任何问题!快速搜索表明您可能想要smtp.gmail.com

请注意,测试 DNS 查询的常用工具是dig,但鉴于您在 Windows 下,您可能会被迫使用它nslookup


推荐阅读