Doctor Octopus
Doctor Octopus

Reputation: 415

AttributeError: 'NoneType' object has no attribute 'text' with for loop

When i run my program:

import requests
from bs4 import BeautifulSoup
class Data():
    def __init__(self):
        self.tags = open("tags.txt", "r")
        self.tag = "everything"
        self.url = "https://www.amazon.com/" + self.tag
        self.headers_param = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36 OPR/74.0.3911.160"}
        self.request = None
        self.soup = None
    def get_data(self):
        for i in range(6):
            self.tag = self.tags.readline()
            self.request = requests.get(self.url, headers=self.headers_param)
            self.soup = BeautifulSoup(self.request.content, "lxml")
            self.finder = self.soup.find("div", {"class":"content"}).text
            print(self.tag)
            print(self.url)
            print(self.finder)
            i += 1
data = Data()
data.get_data()

im getting this error:

Traceback (most recent call last):
  File "D:/Projects/neo_bot/main.py", line 25, in <module>
    data.get_data()
  File "D:/Projects/neo_bot/main.py", line 19, in get_data
    self.finder = self.soup.find("div", {"class":"content"}).text
AttributeError: 'NoneType' object has no attribute 'text'

If i remove text it works great, but i need it. I cant solve my issue. Please help me!

Upvotes: 0

Views: 666

Answers (1)

uingtea
uingtea

Reputation: 6524

as comment above you have two problems

  • get_data() not updating the URL it will always https://www.amazon.com/everything and make your selector not work
  • .find() return None if no element found so you can't use .text, to avoid error check it before

fixed code

class Data():
    def __init__(self):
        self.tags = open("tags.txt", "r")
        self.tag = "everything"
        self.url = "https://www.amazon.com/" + self.tag
        self.headers_param = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36 OPR/74.0.3911.160"}
        self.request = None
        self.soup = None
    def update_url(self):
        self.tag = self.tags.readline()
        self.url = "https://www.amazon.com/" + self.tag
    def get_data(self):
        for i in range(6):
            self.update_url() # <=== update the url
            self.request = requests.get(self.url, headers=self.headers_param)
            self.soup = BeautifulSoup(self.request.content, "lxml")
            self.finder = self.soup.find("div", {"class":"content"})
            if not self.finder: # <== check for NoneType
                print('element not found')
                self.finder = 'no text'
            else:
                self.finder = self.finder.text
            print(self.tag)
            print(self.url)
            print(self.finder)
            i += 1
            
data = Data()
data.get_data()

Upvotes: 1

Related Questions