python - 从 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
解决方案
推荐阅读
- javascript - 可以在将元素返回到新数组之前过滤操作元素吗?
- mysql - Convert SQL Query to ORM Query
- android - 有什么办法可以避免在 Android(Kotlin) 中连续输入相同的代码
- ios - 如果我使用 unwindSegue 从屏幕 A 转到 B 再到 C 又回到 B,如何返回主显示器 (A)
- gradle - 如何在 Gradle 中创建可传递的分层多项目?
- python-3.x - 如何为python中的每个值分配数字
- python - 如何更改空白字符的颜色?
- angular - Angular routing animation: Plays once on refresh
- python - 如何收听任何条目更改事件?
- components - 如何在 Sparx Enterprise Architect (EA) 中对软件组件进行版本控制?