Attribute Error: 'None Type' object has no attribute 'get_text'
Attribute Error: 'None Type' object has no attribute 'get_text'
I have tried this program as I am scrapping data from Amazon but this program giving me the error. instead of get_text I also tried extract() and only strip () also they all are giving Attribute error. Now please help me what should I do?
import urllib.request
from bs4 import BeautifulSoup
import pymysql.cursors
a = input ('enter the item to be searched :')
a = a.replace(" ","")
html = urllib.request.urlopen("https://www.amazon.in/s/ref=nb_sb_noss?url=search-alias%3Daps&field-keywords="+a)
bsObj = BeautifulSoup(html,'lxml')
recordList = bsObj.findAll('a', class_='a-link-normal a-text-normal')
connection = pymysql.connect(host='localhost',
user='root',
password='',
db='shopping',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
try:
with connection.cursor() as cursor:
for record in recordList:
name = record.find("h2", {"class": "a-size-small a-color-base s-inline s-access-title a-text-normal", }).get_text().strip()
sale_price = record.find("span", {"class": "currencyINR"}).get_text().strip()
category = record.find("span", {"class": "a-color-base a-text-bold"}).get_text().strip()
sql = "INSERT INTO `amazon` (`name`, `sale_price`, `category`) VALUES (%s, %s, %s)"
cursor.execute(sql, (name, sale_price, category))
connection.commit()
finally:
connection.close()
record.find
None
1 Answer
1
Like MoxieBall said in the comment above, your call to record.find is returning a None value.
Try checking the value before you call the subsequent .get_text method
Might look something like
raw_sale_price = record.find("span", {"class": "currencyINR"})
if raw_sale_price:
sale_price = raw_sale_price.get_text().strip()
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
This just means that the result of your call to
record.find
isNone
.– MoxieBall
Jun 28 at 16:51