'NoneType' 对象没有属性 'text' 无法让它工作
'NoneType' object has no attribute 'text' can't get it working
我有一些代码可以从供应商处获取新产品。好吧,我刚刚开始编写代码。但是现在我在以下行中得到了 NoneType
:voorraad = result.find('span', {'class': 'po_blok_stoc'}).text
.
它与另一个相同类所以它应该可以工作,对吧?
这是完整代码:
# import libraries
from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup
# specify the url
url = "https://www.erotischegroothandel.nl/nieuweproducten/"
# Connect to the website and return the html to the variable ‘page’
uClient = uReq(url)
page_html = uClient.read()
uClient.close()
# parse the html using beautiful soup and store in variable `soup`
soup = BeautifulSoup(page_html, 'html.parser')
results = soup.find_all('div', {'class': 'po_blok'})
records = []
for result in results:
titel = result.find('span', {'class': 'po_blok_titl'}).text
staat = result.find('span', {'class': 'po_blok_nieu'}).text
voorraad = result.find('span', {'class': 'po_blok_stoc'}).text
records.append((titel, staat, voorraad))
print(records)
这是 html 我从以下位置获得的信息:
<div class="po_blok">
<a href="/aanmelden" class="po_blok_crea">Klant worden</a>
<a href="/login.html" class="po_blok_logi">Al klant? Klik hier om in te loggen</a>
<a href="/massage/massageolie-siliconen/nuru_play_body2body_massage_gel__4l_40589.html">
<img src="https://cdn.edc.nl/250/NGR04000R.jpg" alt="productnaam">
<span class="po_blok_nieu">Nieuw</span>
<span class="po_blok_titl">Nuru Play Body2Body Massage Gel – 4L</span>
<span class="po_blok_stoc">Voorradig</span>
</a>
</div>
原因是其中许多元素是 None
。错误是针对那些元素的,所以我们处理它..
# import libraries
from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup
# specify the url
url = "https://www.erotischegroothandel.nl/nieuweproducten/"
# Connect to the website and return the html to the variable ‘page’
uClient = uReq(url)
page_html = uClient.read()
uClient.close()
# parse the html using beautiful soup and store in variable `soup`
soup = BeautifulSoup(page_html, 'html.parser')
results = soup.find_all('div', {'class': 'po_blok'})
records = []
for result in results:
titel = result.find('span', {'class': 'po_blok_titl'}).text
staat = result.find('span', {'class': 'po_blok_nieu'}).text
voorraad = result.find('span', {'class': 'po_blok_stoc'})
if voorraad:
records.append((titel, staat, voorraad.text))
for record in records:
print(record)
输出:-
('Nuru Play Body2Body Massage Gel – 4L', 'Nieuw', 'Voorradig')
('Nuru Play Body2Body Massage Gel – 335 ml', 'Nieuw', 'Voorradig')
('Nuru Glow Body2Body Massage Gel – 335 ml', 'Nieuw', 'Voorradig')
('P-Trigasm Prostaat Vibrator met Roterende Kralen', 'Nieuw', 'Voorradig')
('Gaia Eco Vibrator - Roze', 'Nieuw', 'Voorradig')
('Gaia Eco Vibrator - Blauw', 'Nieuw', 'Voorradig')
('Zachte Gladde Anaal Dildo', 'Nieuw', 'Voorradig') .etc
我有一些代码可以从供应商处获取新产品。好吧,我刚刚开始编写代码。但是现在我在以下行中得到了 NoneType
:voorraad = result.find('span', {'class': 'po_blok_stoc'}).text
.
它与另一个相同类所以它应该可以工作,对吧?
这是完整代码:
# import libraries
from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup
# specify the url
url = "https://www.erotischegroothandel.nl/nieuweproducten/"
# Connect to the website and return the html to the variable ‘page’
uClient = uReq(url)
page_html = uClient.read()
uClient.close()
# parse the html using beautiful soup and store in variable `soup`
soup = BeautifulSoup(page_html, 'html.parser')
results = soup.find_all('div', {'class': 'po_blok'})
records = []
for result in results:
titel = result.find('span', {'class': 'po_blok_titl'}).text
staat = result.find('span', {'class': 'po_blok_nieu'}).text
voorraad = result.find('span', {'class': 'po_blok_stoc'}).text
records.append((titel, staat, voorraad))
print(records)
这是 html 我从以下位置获得的信息:
<div class="po_blok">
<a href="/aanmelden" class="po_blok_crea">Klant worden</a>
<a href="/login.html" class="po_blok_logi">Al klant? Klik hier om in te loggen</a>
<a href="/massage/massageolie-siliconen/nuru_play_body2body_massage_gel__4l_40589.html">
<img src="https://cdn.edc.nl/250/NGR04000R.jpg" alt="productnaam">
<span class="po_blok_nieu">Nieuw</span>
<span class="po_blok_titl">Nuru Play Body2Body Massage Gel – 4L</span>
<span class="po_blok_stoc">Voorradig</span>
</a>
</div>
原因是其中许多元素是 None
。错误是针对那些元素的,所以我们处理它..
# import libraries
from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup
# specify the url
url = "https://www.erotischegroothandel.nl/nieuweproducten/"
# Connect to the website and return the html to the variable ‘page’
uClient = uReq(url)
page_html = uClient.read()
uClient.close()
# parse the html using beautiful soup and store in variable `soup`
soup = BeautifulSoup(page_html, 'html.parser')
results = soup.find_all('div', {'class': 'po_blok'})
records = []
for result in results:
titel = result.find('span', {'class': 'po_blok_titl'}).text
staat = result.find('span', {'class': 'po_blok_nieu'}).text
voorraad = result.find('span', {'class': 'po_blok_stoc'})
if voorraad:
records.append((titel, staat, voorraad.text))
for record in records:
print(record)
输出:-
('Nuru Play Body2Body Massage Gel – 4L', 'Nieuw', 'Voorradig')
('Nuru Play Body2Body Massage Gel – 335 ml', 'Nieuw', 'Voorradig')
('Nuru Glow Body2Body Massage Gel – 335 ml', 'Nieuw', 'Voorradig')
('P-Trigasm Prostaat Vibrator met Roterende Kralen', 'Nieuw', 'Voorradig')
('Gaia Eco Vibrator - Roze', 'Nieuw', 'Voorradig')
('Gaia Eco Vibrator - Blauw', 'Nieuw', 'Voorradig')
('Zachte Gladde Anaal Dildo', 'Nieuw', 'Voorradig') .etc