Python Forum
Getting 'NoneType' object has no attribute 'find' error when WebScraping with BS
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Getting 'NoneType' object has no attribute 'find' error when WebScraping with BS
#1
Hello,
please help python experts. I want to iterate through multiple pages for products info but when loop gets to the second page of products it throws me 'NoneType' object has no attribute 'find' error. Everything looks find to me though.

from urllib.request import urlopen as uReq
from urllib.request import Request
import requests
from bs4 import BeautifulSoup as soup
pagenumbers = ["1", "2", "3", "4", "5", "6", "7", "8", "9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30"]

url = "https://www.costway.ca"
first_r = requests.get(url)
first_soup = soup(first_r.content,'lxml')
allpage_urls = first_soup.find('div',{'class':'category-content shop-by-catagory'}).find_all('a')
#print(allpage_urls)
for link in allpage_urls:
    originalurl = "https://www.costway.ca" + link['href'] 
    print(originalurl)
    for page in pagenumbers:
        Cat_page_url = originalurl+"?p="+page
        print(Cat_page_url)
        third_r = requests.get(Cat_page_url)
        third_soup = soup(third_r.content,'lxml')
        names=third_soup.find('div',{'class':'pro_datu'}).find_all('a')
        for y in names:
            product_url = y['href']
            print(product_url)
            fourth_r = requests.get(product_url)
            fourth_soup = soup(fourth_r.content,'lxml')
            product_names=fourth_soup.find('div',{'class':'prod_nx'}).find('div',{'class':'orat'}).find('span').find_next_sibling('span')
            Product_number = product_names.text
            print(Product_number)  
https://www.costway.ca/set-of-2-adjustable-pu-leather-backless-bar-stools.html
Item No: 78435109
https://www.costway.ca/29-set-of-2-saddle-nailhead-kitchen-counter-chair.html
Item No: 13678425
https://www.costway.ca/furniture/chairs.html?p=2
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-199-4b0149b6ae67> in <module>
     20             fourth_r = requests.get(product_url)
     21             fourth_soup = soup(fourth_r.content,'lxml')
---> 22             product_names=fourth_soup.find('div',{'class':'prod_nx'}).find('div',{'class':'orat'}).find('span').find_next_sibling('span')
     23             Product_number = product_names.text
     24             print(Product_number)

AttributeError: 'NoneType' object has no attribute 'find'
Thank you very much in advance for looking into it!
Reply
#2
You need to post the entire traceback if you want help, as it gives useful information about the problem (e.g. the line on which the exception was raised). What have you done to debug the problem, though? Something is clearly None so you need to work backwards from there and work out why.
Reply
#3
(Aug-17-2021, 03:42 PM)ndc85430 Wrote: You need to post the entire traceback if you want help, as it gives useful information about the problem (e.g. the line on which the exception was raised). What have you done to debug the problem, though? Something is clearly None so you need to work backwards from there and work out why.


thank you for looking into this ndc85430.

Here is the entire error message from the query:

https://www.costway.ca/set-of-2-adjustable-pu-leather-backless-bar-stools.html
Item No: 78435109
https://www.costway.ca/29-set-of-2-saddle-nailhead-kitchen-counter-chair.html
Item No: 13678425
https://www.costway.ca/furniture/chairs.html?p=2
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-199-4b0149b6ae67> in <module>
     20             fourth_r = requests.get(product_url)
     21             fourth_soup = soup(fourth_r.content,'lxml')
---> 22             product_names=fourth_soup.find('div',{'class':'prod_nx'}).find('div',{'class':'orat'}).find('span').find_next_sibling('span')
     23             Product_number = product_names.text
     24             print(Product_number)

AttributeError: 'NoneType' object has no attribute 'find'
Well, I understand that error message indicates that there is none in the page when loop gets to the next page. Though when I run the query only for that next page result is successful. That is why I am puzzled here.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Bug TypeError: 'NoneType' object is not subscriptable TheLummen 4 735 Nov-27-2023, 11:34 AM
Last Post: TheLummen
  Error: audioio has no attribute 'AudioOut' netwrok 3 628 Oct-22-2023, 05:53 PM
Last Post: netwrok
  TypeError: 'NoneType' object is not callable akbarza 4 983 Aug-24-2023, 05:14 PM
Last Post: snippsat
  AttributeError: '_tkinter.tkapp' object has no attribute 'username' Konstantin23 4 1,666 Aug-04-2023, 12:41 PM
Last Post: Konstantin23
  Need help with 'str' object is not callable error. Fare 4 824 Jul-23-2023, 02:25 PM
Last Post: Fare
  Python: Regex is not good for re.search (AttributeError: 'NoneType' object has no att Melcu54 9 1,460 Jun-28-2023, 11:13 AM
Last Post: Melcu54
  Python: AttributeError: 'PageObject' object has no attribute 'extract_images' Melcu54 2 3,859 Jun-18-2023, 07:47 PM
Last Post: Melcu54
  Object attribute behavior different in 2 scripts db042190 1 728 Jun-14-2023, 12:37 PM
Last Post: deanhystad
  cx_oracle Error - AttributeError: 'function' object has no attribute 'cursor' birajdarmm 1 2,319 Apr-15-2023, 05:17 PM
Last Post: deanhystad
  Pandas AttributeError: 'DataFrame' object has no attribute 'concat' Sameer33 5 5,581 Feb-17-2023, 06:01 PM
Last Post: Sameer33

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020