我在运行此代码时遇到名称错误

时间:2019-11-07 18:19:52

标签: python-3.x request

import requests
from bs4 import BeautifulSoup as bs

session = request.session()

def get_sizes_in_stock():
    global session
    endpoint = "https://www.off---white.com/en/SE/men/products/omia065r208000010100#"
    response = session.get(endpoint)

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

    ul = soup.find("ul",{"class":"styled-radio"})
    all_sizes = ul.find_all("li")

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

    return sizes_in_stock

print(get_sizes_in_stock())

收到的错误是:

Traceback (most recent call last):
  File "C:\Users\arvid\Desktop\python tings\owbot.py", line 4, in <module>
    session = request.session()
NameError: name 'request' is not defined
>>> 

1 个答案:

答案 0 :(得分:0)

您在requests中拼写了session = request.session()

该库为requests(复数)