Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
124 views
in Technique[技术] by (71.8m points)

python - How to extract this text from this Website Scraping? BeatifulSoup

I'm trying to extract the string "No disponble" of this product, but im getting "none" or attribute text dont exists:

enter image description here

https://es.louisvuitton.com/esp-es/productos/neceser-26-monogram-canvas-000767#M47542

disponibilidad = soup.find("span", {"class":"lv-stock-indicator lv-product-stock-indicator lv- 
product__stock list-label-m -not-available"}).text

But im getting this error:

AttributeError: 'NoneType' object has no attribute 'text'
question from:https://stackoverflow.com/questions/65853207/how-to-extract-this-text-from-this-website-scraping-beatifulsoup

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As mentioned in the comments content is dynamically generated but you can go with selenium.

Example

from selenium import webdriver
from bs4 import BeautifulSoup
from time import sleep

driver = webdriver.Chrome(executable_path='C:Program FilesChromeDriverchromedriver.exe')

url = 'https://es.louisvuitton.com/esp-es/productos/neceser-26-monogram-canvas-000767#M47542'
driver.get(url)
sleep(2)
soup = BeautifulSoup(driver.page_source, "lxml")

print(soup.select_one('div.lv-product__price-stock > span')).get_text()

driver.close()

Output

No disponible


Get in stock information without selenium

If you just interessted if product is available you can also requests the api with the sku of the product (hint and homework - last part of your url :)

Example

import requests, json
from time import sleep
headers = {"user-agent": "Mozilla/5.0"}

skuList = ['M47542', 'M45587', '1A8EMO']
availabilities = {}

for id in idList:
    r=requests.get('https://api.louisvuitton.com/api/esp-es/catalog/availability/{0}'.format(id),headers=headers)
    availabilities[id] = (r.json()['skuAvailability'])
    print(r.json()['skuAvailability'])
    sleep(4)

Output availabilities

{'000767': [{'skuId': 'M47542',
   'exists': True,
   'backOrder': False,
   'inStock': False}],
 'nvprod2540049v': [{'skuId': 'M45587',
   'exists': True,
   'backOrder': False,
   'inStock': False},
  {'skuId': 'M45586', 'exists': True, 'backOrder': False, 'inStock': False}],
 'nvprod2470212v': [{'skuId': '1A8EMO',
   'exists': True,
   'backOrder': False,
   'inStock': False},
  {'skuId': '1A8EMQ', 'exists': True, 'backOrder': False, 'inStock': False},
  {'skuId': '1A8EMS', 'exists': True, 'backOrder': False, 'inStock': False},
  {'skuId': '1A8EMU', 'exists': True, 'backOrder': False, 'inStock': False},
  {'skuId': '1A8EMW', 'exists': True, 'backOrder': False, 'inStock': False},
  {'skuId': '1A8EMY', 'exists': True, 'backOrder': False, 'inStock': False},
  {'skuId': '1A8EN0', 'exists': True, 'backOrder': False, 'inStock': False},
  {'skuId': '1A8EM9', 'exists': True, 'backOrder': False, 'inStock': False},
  {'skuId': '1A8EMB', 'exists': True, 'backOrder': False, 'inStock': True},
  {'skuId': '1A8EMD', 'exists': True, 'backOrder': False, 'inStock': False},
  {'skuId': '1A8EMF', 'exists': True, 'backOrder': False, 'inStock': True},
  {'skuId': '1A8EMH', 'exists': True, 'backOrder': False, 'inStock': False},
  {'skuId': '1A8EMJ', 'exists': True, 'backOrder': False, 'inStock': False},
  {'skuId': '1A8EML', 'exists': True, 'backOrder': False, 'inStock': False}]}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...