'Running my Python script returns "NoneType"
My code returns NoneType when I add .text it returns NoneType and when I delete .text it return None in dataframe. How can I resolve this issue?
data_adi = []
n=0
for n in range(pagenum):
pages_url = f"https://www.adiglobaldistribution.us/search?page={n+1}&criteria=Tp- link%20Usa%20Corporation"
driver.get(pages_url)
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, '//* [@id="nwrap"]/div/div/div[2]/div/div/div/div/div[3]/div/div/div/div[1]/div[6]/div/div[2]/ul')))
time.sleep(5)
html = driver.page_source
soup = Soup(html)
for item in soup.select("div", class_='[class="flex-container"]'):
data_adi.append({
'title' : item.find("div", class_="rd-item-name").text,
'name' : item.find("span", class_='item-num-mfg').text,
'link' : item.find("div", class_="rd-item-name", href=True)['href'],
'price' : item.find("div", class_="pdp-price-wrapper").text,
'stock' : item.find("span", class_="availabilityMessage-rd").text
})
df_adi = pd.DataFrame(data_adi)
df_adi.drop_duplicates()
df_adi
Solution 1:[1]
You first need to make sure, element is not Null.
node = child.find('title')
if node is not None:
= node.text
else:
= None
Or use build in Function getattr:
... = getattr(child.find('title'), 'text', None)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |

