问题描述
我正在尝试抓取一个网页来分析一些鞋子的库存,我按照自己的意愿做了,但我需要添加一件事,例如,当突然有一个鞋子的库存时鞋码,它向我发送了一个尺寸为有库存的 Discord webhook,但它一直向我发送 webhook 直到它停止有库存,问题是当它实现 if 的功能时我想这样做,它发送webhook,但是下次我检查如果库存仍然有库存,请不要发送任何webhook,直到它缺货然后再重新入库,我不知道我是否解释得很好.
I am trying to scrape a web page to analyze the stock of some shoes, I did it as I would like but I would need to add one thing, and that is, for example, when suddenly there is a stock of a shoe size, it sends me a Discord webhook with the size that there is stock, but it keeps sending me webhooks all the time until it stops having stock, the thing is that I would like to do that when it fulfills the function of the if, it sends the webhook, but the next time I check If the stock is still in stock, do not send me any webhook until it is out of stock and then back in stock, I don't know if I have explained myself well.
我希望它只执行一次,即使它继续执行该功能,也可以执行该功能,只有在库存状态恢复为缺货然后有库存时才再次执行该功能
I want it to fulfill the function once it only executes it once even though it continues to fulfill that function, only to fulfill it again when the stock status returns to out of stock and then to with stock
这是我的代码:
from bs4 import BeautifulSoup
from dhooks import Webhook, Embed
import requests
import pandas as pd
import logging
from json import loads
import time, datetime
import random
from requests.auth import HTTPProxyAuth
import time
import multiprocessing
import re
headers = {
}
def monitor2(url):
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.content, 'html.parser')
marca = soup.find("h3", {"class":"OEhtt9 ka2E9k uMhVZi uc9Eq5 pVrzNP _5Yd-hZ"}).text
nombre = soup.find("h1", {"class":"OEhtt9 ka2E9k uMhVZi z-oVg8 pVrzNP w5w9i_ _1PY7tW _9YcI4f"}).text
color = soup.find("span", {"class":"u-6V88 ka2E9k uMhVZi dgII7d z-oVg8 pVrzNP"}).text
precio = soup.find("span", {"class":"uqkIZw ka2E9k uMhVZi FxZV-M z-oVg8 pVrzNP"}).text
talla = soup.find("span", {"class":"u-6V88 ka2E9k uMhVZi FxZV-M z-oVg8 pVrzNP"}).text
imagen = soup.find("img", {"class": "_6uf91T z-oVg8 u-6V88 ka2E9k uMhVZi FxZV-M _2Pvyxl JT3_zV EKabf7 mo6ZnF _1RurXL mo6ZnF PZ5eVw"})['src']
api = 'https://api.silverpings.eu/zalando?skuid='
tallas = soup.find_all(re.compile("script"))[15]
tallas2 = re.findall(r'size":.....', str(tallas))
tallas3 = str(tallas2).replace('size":"',"").replace('"', "").replace(']', "").replace("'", "").replace(",", "").replace("[Te", "").replace("r", "").replace("tall", "").replace("¿Cuá", "").replace("}", "").split()
tallas3 = sorted(list(set(tallas3)))
skus = soup.find_all(re.compile("script"))[15]
skus2 = re.findall(r"sku.......................", str(skus))
skus3 = str(skus2).replace('sku":"',"").replace("'", "").replace("'","").replace("[","").replace("]","").replace("silh", "").replace("uri", "").replace(",", "").replace(" ", "")
skus4 = str(skus3).strip()
skus5 = re.findall(r".........-...\d......", str(skus4))
disponibilidad = soup.find_all(re.compile("script"))[15]
disponibilidad2 = re.findall(r'quantity":.............', str(disponibilidad))
disponibilidad3 = str(disponibilidad2).replace('quantity":"',"").replace('"', "").replace(']', "").replace("'", "").replace(",", "").replace("[", "").replace("r", "").replace("tall", "").replace("¿Cuá", "").replace("}", "").split()
print("[",datetime.datetime.now().hour,":",datetime.datetime.now().minute,":",datetime.datetime.now().second,":",datetime.datetime.now().microsecond,"]", " Comprobando disponibilidad de: ("+nombre+")")
numero = 0
while numero <= len(tallas3):
if not "OUT_OF_STOCK" in disponibilidad3[numero]:
hook = Webhook('')
embed = Embed(
color=15105570,
timestamp='now' # sets the timestamp to current time
)
embed.url = (url)
embed.title = (nombre+" " + color)
embed.add_field(name='Talla', value="["+tallas3[numero]+"]("+api+skus5[numero]+")")
embed.add_field(name='Precio', value=precio)
embed.add_field(name='Useful Links', value="[Checkout](https://www.zalando.es/checkout/confirm)", inline=False)
embed.set_footer(text='Zalando by SilverPings', icon_url="https://assets.stickpng.com/thumbs/5a32a860cb9a85480a628f95.png")
embed.set_thumbnail(imagen)
hook.send(embed=embed)
print("[",datetime.datetime.now().hour,":",datetime.datetime.now().minute,":",datetime.datetime.now().second,"]", " Stock encontrado: ("+nombre + " - " + tallas3[numero]+")")
numero = numero+1
else:
numero = numero+1
if numero == len(tallas3):
break
def gymred():
url = 'https://www.zalando.es/nike-sportswear-air-force-1-07-zapatillas-light-bonewhite-ni112o0h3-
a12.html'
monitor2(url)
while True:
gymred()
推荐答案
您可以创建一个在发送 webhook 时变为 True 的布尔值,然后查找该值为 false 以发送电子邮件.代码应该是这样的:
You can create a boolean value that becomes True when you send the webhook, and then look for this value to be false in order to send the email. The code would be something like this:
if not "OUT_OF_STOCK" in disponibilidad3[numero] and not enviado:
enviado = True
hook = Webhook('')
embed = Embed(
color=15105570,
timestamp='now' # sets the timestamp to current time
)
embed.url = (url)
embed.title = (nombre+" " + color)
embed.add_field(name='Talla', value="["+tallas3[numero]+"]("+api+skus5[numero]+")")
embed.add_field(name='Precio', value=precio)
embed.add_field(name='Useful Links', value="[Checkout](https://www.zalando.es/checkout/confirm)", inline=False)
embed.set_footer(text='Zalando by SilverPings', icon_url="https://assets.stickpng.com/thumbs/5a32a860cb9a85480a628f95.png")
embed.set_thumbnail(imagen)
hook.send(embed=embed)
print("[",datetime.datetime.now().hour,":",datetime.datetime.now().minute,":",datetime.datetime.now().second,"]", " Stock encontrado: ("+nombre + " - " + tallas3[numero]+")")
numero = numero+1
else:
numero = numero+1
if numero == len(tallas3):
break
如果你想发送另一个网络钩子,你可以更新这个布尔值,但我会把它留给你.
Then you could update this boolean value if you want to send another webhook but I will leave that to you.
这篇关于只执行一个 IF 直到值被改变 Scraping的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!