RRUDARY
RRUDARY

Reputation: 71

Object Has No Attribute 'Text'

Ok, let's try this again. I am scraping a webpage that is in xml format. I am gathering what I need, but for one item it is unable to pull the text (dubbed 'item' in my code below). I am getting the following error: "item = items.find("image:title").text AttributeError: 'NoneType' object has no attribute 'text'" I would just like to get the text for 'item'.

Here's my code:

import requests
from bs4 import BeautifulSoup

headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64)  AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36'}

url = 'https://www.kith.com/sitemap_products_1.xml'

r = requests.get(url=url, headers=headers)

soup = BeautifulSoup(r.text, 'html.parser')

for items in soup.find_all("url"):
    item = items.find("image:title").text
    url = items.find("loc").text
    if item is not None:
        print(item, url)

Upvotes: 0

Views: 14180

Answers (2)

Omer Tekbiyik
Omer Tekbiyik

Reputation: 4744

Your first text return None so you get this error . You need to check item is none or not before trying to get text.

for items in soup.find_all("url"):
getTitle = items.find('image:title')
if getTitle is not None:
    item = getTitle.text
    url = items.find("loc").text
    print (item,url)

Upvotes: 1

spaniard
spaniard

Reputation: 560

Basically in this line:

item = items.find("image:title").text 

items.find("image:title") return None (probably because find does not find what you expect in items). So then as None does not have the attribute text then (None).text raises the error AttributeError: 'NoneType' object has no attribute 'text'

If you want to fix the error, you can do:

item = items.find("image:title")
if item:
    title = item.text     # you can use other variable name if you want to.
else:
    print("there is no image:title in items")

Upvotes: 1

Related Questions