【发布时间】:2021-01-17 18:24:37
【问题描述】:
我正在尝试抓取一个网页来分析一些鞋子的库存,我按照我的意愿做了,但我需要添加一件事,例如,当突然有一个库存鞋码,它向我发送了一个有库存尺寸的 Discord webhook,但它一直向我发送 webhook 直到它停止有库存,问题是当它满足 if 的功能时我想这样做,它会发送webhook,但是下次我检查是否有库存时,请不要向我发送任何webhook,直到缺货然后再重新入库,我不知道我是否解释得很好。
我希望它只执行一次就完成该功能,即使它继续执行该功能,仅在库存状态返回到缺货然后再到有库存时再次执行它
这是我的代码:
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()
【问题讨论】:
标签: python web-scraping