Attribute Error: 'None Type' object has no attribute 'get_text'
Attribute Error: 'None Type' object has no attribute 'get_text'
我尝试过这个程序,因为我正在从亚马逊抓取数据,但这个程序给我错误。而不是 get_text 我也尝试了 extract() 并且只尝试了 strip () 而且它们都给出了属性错误。现在请帮助我该怎么办?
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()
就像 MoxieBall 在上面的评论中所说的那样,您对 record.find 的调用返回了一个 None 值。
在调用后续 .get_text 方法
之前尝试检查该值
可能看起来像
raw_sale_price = record.find("span", {"class": "currencyINR"})
if raw_sale_price:
sale_price = raw_sale_price.get_text().strip()
我尝试过这个程序,因为我正在从亚马逊抓取数据,但这个程序给我错误。而不是 get_text 我也尝试了 extract() 并且只尝试了 strip () 而且它们都给出了属性错误。现在请帮助我该怎么办?
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()
就像 MoxieBall 在上面的评论中所说的那样,您对 record.find 的调用返回了一个 None 值。 在调用后续 .get_text 方法
之前尝试检查该值可能看起来像
raw_sale_price = record.find("span", {"class": "currencyINR"})
if raw_sale_price:
sale_price = raw_sale_price.get_text().strip()