There is a website with this html-code and only one itemprop="brand":
<dd itemprop="brand" class="attributeValue-2574930263">
<a class="link-3970392289 link__default-1151936189 attributeLink-2414863004" href="/b-
cars-trucks/ottawa/hyundai/c174l1700185a54">Hyundai</a>
</dd>
I can not use classes, because there are many of theme.
I am trying to get the text (Hyundai) with this code:
def get_car_datas(url):
req = requests.get(url, headers=headers)
src = req.text
soup = BeautifulSoup(src, "lxml")
car_brand = soup.find(itemprop_="brand").next_elements
print(car_brand)
I get this message:
AttributeError: ‘NoneType’ object has no attribute ‘next_elements’
How can I get the text Hyundai?
4
Answers
I am trying with only one page. Some times the respond is the text I am searching for (Hyundai, BMW, Kia...), some times it is responding "Brand not found :(". Here is the full code:
You have a typo in the find() method. Instead of using itemprop_="brand", you should use itemprop="brand".
this error means that Beautifulsoup is not able to find the given element.
Try this:
This error is caused because
soup.find(itemprop_="brand")
has not found any element withitemprop_="brand"
, therefore returning None. Since None does not have an attribute callednext_elements
, this error is thrown.First of all, you have a type, as mentioned by Teroaz. You are searching for
itemprop
, notitemprop_
. So you could do this (but you have another option below):This might be more explicit on what you are searching for, so you could do this:
It may work better. Or, if you’d like, you could stick to the option above.