-1

So I want to create web scraper that will work from inputting URL, classes, elements and headers from console. I want to put e and f variable in d = list.find(g, class_=h).text.replace('\n', '') I came up with this code, but it gives me error: AttributeError: 'NoneType' object has no attribute 'text'. Seems like I have something incorrect in here: d = list.find(g, class_=h).text.replace('\n', '')

from pydoc import classname
from bs4 import BeautifulSoup
import requests
from csv import writer

header = []
element = []
classname = []

url = input("URL: ")
page = requests.get(str(url))

soup = BeautifulSoup(page.content, 'html.parser')
x = input("Enter element of first class (e.g div): ")
y = input("Enter class name for that element: ")
lists = soup.find_all(str(x), class_=str(y))
z = input("Enter file name you want to write data in (must end with .csv): ")

a =" "
while a!= "":
    a = input("Enter headers: ")
    if a!= "":
        header.append(a)
        print(header)

with open(str(z), 'w', encoding='utf8', newline='') as f:
    thewriter = writer(f) 
    thewriter.writerow(header)
    c = len(header)
    for  i in range(c):
        e = input("Enter element of first class (e.g div): ")
        element.append(e)
        f = input("Enter class name for that element: ")
        classname.append(f)
    for list in lists:
        for i in range(c):
            d = header[i]
            g = e[i]
            h = f[i]
            d = list.find(g, class_=h).text.replace('\n', '')
            info = d
            thewriter.writerow(info)
  • This `d = list.find(g, class_=h)` is returning None, so when you call .text.replace on it the method is not valid – Ollie Graham Feb 24 '22 at 13:49
  • how to write it in correct form – Nikoloz Mchedlishvili Feb 24 '22 at 14:00
  • Does this answer your question? [AttributeError: 'NoneType' object has no attribute 'text' , I don't understand how to fix it](https://stackoverflow.com/questions/69859239/attributeerror-nonetype-object-has-no-attribute-text-i-dont-understand-h) – Tomerikoo Feb 24 '22 at 14:08

1 Answers1

0

Because soup.find_all returns you need to loop through it and use text method Or look at this question AttributeError: 'NoneType' object has no attribute 'text' , I don't understand how to fix it

Devil Ishere
  • 185
  • 1
  • 13