我目前正在嘗試使用smtp從我的 gmail 向另一個雅虎電子郵件地址發送電子郵件。我從我的 Gmail 帳戶啟用了安全性較低的應用程式。我也經歷過 Gmail 驗證碼。我還嘗試添加埠號(587、2525),但沒有成功。我無法讓代碼作業。我在這里發布了我的代碼,然后是錯誤訊息。
奇怪的是,幾個月前這曾經對我有用!!
import requests
from bs4 import BeautifulSoup
# import lxml
import smtplib
my_email = "[email protected]"
password = "MY_PASSWORD"
URL = "https://www.amazon.com/Instant-Pot-Plus-60-Programmable/dp/B01NBKTPTS/ref=sr_1_1_sspa?crid=34PO9PKK0CUSX" \
"&keywords=instant+pot&qid=1639842722&sprefix=instant,aps,76&sr=8-1-spons&spLa=ZW5jcnlwdGVkUXVhbGlmaWV" \
"yPUEyUlM1TDQ5N0RQS1VXJmVuY3J5cHRlZElkPUEwNzc4NTk5M0c2VVhTVlE1Q1lBTyZlbmNyeXB0ZWRBZElkPUEwMjE0NzMyNUlTVUg0V1" \
"BYQTZWJndpZGdldE5hbWU9c3BfYXRmJmFjdGlvbj1jbGlja1JlZGlyZWN0JmRvTm90TG9nQ2xpY2s9dHJ1ZQ&th=1 "
TARGET_PRICE = 200
headers = {
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36",
"Accept-Language": "en-GB,en-US;q=0.9,en;q=0.8"
}
response = requests.get(URL, headers=headers)
website_html = response.text
print(response.status_code)
soup = BeautifulSoup(website_html, "lxml")
price = float(soup.find(id="attach-base-product-price").attrs["value"])
title = soup.find(id="productTitle").get_text()
# sending a notification email
if price <= TARGET_PRICE:
message = f"{title} is now {price}"
with smtplib.SMTP("smtp.gmail.com") as connection:
connection.starttls()
result = connection.login(my_email, password)
connection.sendmail(
from_addr=my_email,
to_addrs="[email protected]",
msg=f"Subject:Amazon Price Alert!\n\n{message}\n{URL}"
)
print("done")
錯誤資訊
Traceback (most recent call last):
File "/Users/Charaf/PycharmProjects/Intermediate /AmazonPriceTracker/main.py", line 45, in <module>
with smtplib.SMTP("smtp.gmail.com") as connection:
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/smtplib.py", line 255, in __init__
(code, msg) = self.connect(host, port)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/smtplib.py", line 341, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/smtplib.py", line 312, in _get_socket
return socket.create_connection((host, port), timeout,
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/socket.py", line 843, in create_connection
raise err
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/socket.py", line 831, in create_connection
sock.connect(sa)
TimeoutError: [Errno 60] Operation timed out
uj5u.com熱心網友回復:
您是否嘗試過使用 Google 應用密碼 ( https://support.google.com/accounts/answer/185833?hl=en )?我剛剛用我的亞馬遜價格檢查器測驗了它,它非常相似,它適用于我的,帶有谷歌應用程式密碼。
這是我通常用來發送郵件的內容 -
def Send_Mail(email,password,remail): #Function for sending a mail to the user
server = smtplib.SMTP('smtp.gmail.com',587)
server.ehlo()
server.starttls()
server.ehlo()
server.login(email,password)
body = f"Price Fell Down\n\nCheck -{url}"
server.sendmail(email,remail,body)
uj5u.com熱心網友回復:
默認情況下,Gmail 使用 IMPLICIT TLS 連接 (SSL)。
這意味著您不會使用starttls(). 相反,您連接到不同的埠;ssl 埠465。
消除:
server.starttls()
server.ehlo()
..并且只保留一個server.ehlo(),當然在埠上連接465。
在下面的串列中,您的代碼應該適用于 Gmail 和 Rackspace 以外的任何服務。
- GMAIL.COM - 隱式 TLS 連接 (SSL)
- RACKSPACE.COM - 隱式 TLS 連接 (SSL)
- MAILTRAP.COM - 顯式 TLS 連接 (STARTTLS)
- iCLOUD.COM - 顯式 TLS 連接 (STARTTLS)
- OUTLOOK - 顯式 TLS 連接 (STARTTLS)
- OUTLOOK.COM - 顯式 TLS 連接 (STARTTLS)
- YAHOO.COM - 顯式 TLS 連接 (STARTTLS)
- MAIL.COM - 顯式 TLS 連接 (STARTTLS)
- AOL.COM - 顯式 TLS 連接 (STARTTLS)
- COMCAST.NET - 顯式 TLS 連接 (STARTTLS)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/399438.html
