Python : 在调用 send_hook 相同项目的 Discord 之前等待一段时间
Python : Wait certain time before calling the send_hook Discord for the same item
我的程序通过 URL 检查许多商品,并在商品有货时调用 send_hook。
我希望我的程序在再次为同一项目调用 send_hook 之前等待一段时间。然而,它应该继续检查其他 URL。
.. 这可能吗?
这是我的程序:
from bs4 import BeautifulSoup
from discord import send_hook
from datetime import datetime
pid = ['PB00393363','PB00430691','PB00430699']
headers = {
'user-agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.85 Safari/537.36'
}
while True:
for i in range(len(pid)):
url = "https://www.ldlc.com/fr-be/fiche/{}.html".format(pid[i])
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, 'html.parser')
soup = BeautifulSoup(requests.get(url).content, 'html.parser')
stock_statut = soup.find('div', class_='content').get_text().strip()
product_title = soup.find('h1').get_text().strip()
price = soup.find_all('div', class_='price')[3].get_text()
try:
image_url = soup.find('img', {'id':'ctl00_cphMainContent_ImgProduct'})['src']
except:
image_url = soup.find('img', {'id':'ctl00_cphMainContent_ImgProduct'})['src']
if stock_statut == 'En stock':
print('[{}] En stock'.format(str(datetime.now())))
send_hook(product_title, url, image_url, price)
else:
print('[{}] Rupture'.format(str(datetime.now())))
如果你能帮助我,我会很高兴。
谢谢!
您将需要使用 python 时间模块。
我只是复制了整个代码并添加了一些行来帮助你。
from bs4 import BeautifulSoup
from discord import send_hook
from datetime import datetime
import time
# Declare a wait time in seconds
wait_time = 30
pid = ['PB00393363','PB00430691','PB00430699']
# Use a dictionary associate each item with the time of the last find
# Init with 0 meaning long in the past
timed_pid = {p: 0 for p in pid}
...
while True:
# Wait some time. Multiple requests per second could be considered a DOS attack
time.sleep(5)
# do not iterate over the length
#for i in range(len(pid)):
# iterate the keys of the dict
for p in timed_pid:
# check the time passed since the last find
if (time.time() - timed_pid[p]) < wait_time:
# wait time has not passed: skip
continue
# use p directly for formating
url = "https://www.ldlc.com/fr-be/fiche/{}.html".format(p)
...
if stock_statut == 'En stock':
print('[{}] En stock'.format(str(datetime.now())))
# set the time of the current find
timed_pid[p] = time.time()
send_hook(product_title, url, image_url, price)
else:
print('[{}] Rupture'.format(str(datetime.now())))
我的程序通过 URL 检查许多商品,并在商品有货时调用 send_hook。 我希望我的程序在再次为同一项目调用 send_hook 之前等待一段时间。然而,它应该继续检查其他 URL。 .. 这可能吗?
这是我的程序:
from bs4 import BeautifulSoup
from discord import send_hook
from datetime import datetime
pid = ['PB00393363','PB00430691','PB00430699']
headers = {
'user-agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.85 Safari/537.36'
}
while True:
for i in range(len(pid)):
url = "https://www.ldlc.com/fr-be/fiche/{}.html".format(pid[i])
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, 'html.parser')
soup = BeautifulSoup(requests.get(url).content, 'html.parser')
stock_statut = soup.find('div', class_='content').get_text().strip()
product_title = soup.find('h1').get_text().strip()
price = soup.find_all('div', class_='price')[3].get_text()
try:
image_url = soup.find('img', {'id':'ctl00_cphMainContent_ImgProduct'})['src']
except:
image_url = soup.find('img', {'id':'ctl00_cphMainContent_ImgProduct'})['src']
if stock_statut == 'En stock':
print('[{}] En stock'.format(str(datetime.now())))
send_hook(product_title, url, image_url, price)
else:
print('[{}] Rupture'.format(str(datetime.now())))
如果你能帮助我,我会很高兴。 谢谢!
您将需要使用 python 时间模块。
我只是复制了整个代码并添加了一些行来帮助你。
from bs4 import BeautifulSoup
from discord import send_hook
from datetime import datetime
import time
# Declare a wait time in seconds
wait_time = 30
pid = ['PB00393363','PB00430691','PB00430699']
# Use a dictionary associate each item with the time of the last find
# Init with 0 meaning long in the past
timed_pid = {p: 0 for p in pid}
...
while True:
# Wait some time. Multiple requests per second could be considered a DOS attack
time.sleep(5)
# do not iterate over the length
#for i in range(len(pid)):
# iterate the keys of the dict
for p in timed_pid:
# check the time passed since the last find
if (time.time() - timed_pid[p]) < wait_time:
# wait time has not passed: skip
continue
# use p directly for formating
url = "https://www.ldlc.com/fr-be/fiche/{}.html".format(p)
...
if stock_statut == 'En stock':
print('[{}] En stock'.format(str(datetime.now())))
# set the time of the current find
timed_pid[p] = time.time()
send_hook(product_title, url, image_url, price)
else:
print('[{}] Rupture'.format(str(datetime.now())))