为什么我的代码没有返回正确的输出?

时间:2019-05-17 17:48:41

标签: python

我希望我的代码可以输出网站上剩余的库存,但是当我运行代码时,我得到的是:[]

似乎没有输出打印命令,但输出的是sizes_in_stock变量

import select
import requests
from bs4 import BeautifulSoup as bs
import random


session = requests.session()


def get_sizes_in_stock():
    global session
    endpoint = 'https://www.supremenewyork.com/shop/tops-sweaters/v4j0kn2qc/vtl376a1b'
    response = session.get(endpoint)

    soup = bs(response.text, 'html.parser')

    div = soup.find("select", {"name": "s"})
    div = soup.find("select", {"name": "s"})
    all_sizes = soup.find_all("option")

    sizes_in_stock = []
    for size in all_sizes:
        if "false" not in size["class"]:
            size_id = size ["value"]
        sizes_in_stock.append(size_id.split("_")[1])

    return sizes_in_stock


print(get_sizes_in_stock())

def add_to_cart():
    global session

    sizes_in_stock = get_sizes_in_stock()
    size_chosen = random.choice(sizes_in_stock)

    endpoint = 'https://www.supremenewyork.com/shop/%s/add' % (size_chosen)
    print(endpoint)

    response = session.get(endpoint)


#add_to_cart()

0 个答案:

没有答案