Как можно специально добавить [none]
, если элемент не найден? У меня есть элемент, который иногда существует, а иногда нет. ( ССЫЛКА ЗДЕСЬ )
ниже токового выхода в df
:
name tag
ZX Torsion Releasing Soon
Campus Restock
Campus Restock
Consortium Runner Mid 4D Sold out
Ozweego Sold out
Ozweego Sold out
Yeezy Boost 350 V2 Infant Sold out
Yeezy Boost 350 V2 Kids Sold out
Yeezy Boost 350 V2 Sold out
Yung-1 Sold out
Yung 1 Sold out
A.R. Trainer Sold out
A.R. Trainer Sold out
Желаемый выход
name tag
ZX Torsion Releasing Soon
Campus Restock
Campus Restock
Consortium Runner Mid 4D null
Ozweego null
Ozweego null
Yeezy Boost 350 V2 Infant Sold out
Yeezy Boost 350 V2 Kids Sold out
Yeezy Boost 350 V2 Sold out
Yung-1 null
Yung 1 null
A.R. Trainer null
A.R. Trainer null
....and so on
Рабочий код:
import requests
from bs4 import BeautifulSoup as bs
import pandas as pd
urls = [
'https://www.nakedcph.com/sneakers-by-adidas/s/37'
]
baseURL = 'https://www.nakedcph.com'
final = []
with requests.Session() as s:
for url in urls:
driver = webdriver.Chrome('/Users/Documents/python/Selenium/bin/chromedriver')
driver.get(url)
soup = bs(driver.page_source, 'lxml')
items = soup.findAll("div", {"class" : lambda L: L and L.startswith('col-6 col-md-3 mb-5')})
name = [item.find('span',{'class':'product-name d-block'}).text.strip() for item in items]
tag = [item.find('svg').next_sibling.strip() for item in soup.findAll('div',{'class':'card-ribbon'})]
results = list(zip(name,tag))
df = pd.DataFrame(results)
driver.quit()
df