Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help to resolve this
#1
I'm new to Phyton and do not have any Programming background.
I downloaded Phyton 3.8 and PYcharm. I tried to work the program below and am getting the below error
AttributeError: 'NoneType' object has no attribute 'get_text'

Looks like no values are returned by soup.find statement, how to proceed further? Thank you

import requests
from bs4 import BeautifulSoup

URL = 'https://www.amazon.com/Disposable-Offices-Households-Sensitive-Crowded/dp/B085TF7JPK/ref=sr_1_1?dchild=1&keywords=masks&qid=1585366634&sr=8-1'
headers = {"User-Agent" : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36' }

page = requests.get(URL , headers=headers)

soup = BeautifulSoup(page.content, "html.parser")

print(soup.prettify())

title = soup.find(id = "productTitle" ).get_text()
price = soup.find(id = "priceblock_ourprice" ).get_text()

print(title)
print(price)
Reply
#2
I don't know anything about working with requests and BeautifulSoup, but, I would say to put the statements in a try except statement like this.
try:
    title = soup.find(id = "productTitle" ).get_text()
except:
    print("Title not found")
try:
    price = soup.find(id = "priceblock_ourprice" ).get_text()
except:
    print("Title not found")
If you are having to iterate that over and over then I would suggest making it a function
def FindById(ID):
    try:
        return soup.find(id = ID).get_text()
    except:
        print("Error: could not find")

title = FindById("productTitle")
price = FindById("priceblock_ourprice")
Hopefully this helps
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [split] How to resolve version conflicts in Python? atonalwilson 1 952 May-04-2023, 09:02 AM
Last Post: buran
  How to resolve version conflicts in Python? taeefnajib 0 873 Apr-27-2023, 08:37 PM
Last Post: taeefnajib
  How to resolve my problem in Pycharm? bshoushtarian 0 821 Sep-26-2022, 11:45 AM
Last Post: bshoushtarian
  Solving equation equal to zero: How to resolve the syntax error? alexfrol86 3 1,896 Feb-21-2022, 08:58 AM
Last Post: deanhystad
  win32com — How to resolve “AttributeError: xlUp” for Excel files? JaneTan 2 4,129 Aug-18-2021, 05:27 AM
Last Post: snippsat
  How to resolve Index Error in my code? codify110 6 2,957 May-22-2021, 11:04 AM
Last Post: supuflounder
  How to resolve numpy ValueError: dtype.descr Py_veeran 0 1,817 Aug-18-2020, 06:46 PM
Last Post: Py_veeran
  wrap_text with openpyxl. How to use documentation to resolve deprecation warning? curranjohn46 4 14,222 Oct-09-2019, 01:04 PM
Last Post: curranjohn46
  How to resolve 404 returned from web method when not running on localhost littleGreenDude 3 3,027 Feb-05-2019, 09:01 PM
Last Post: littleGreenDude
  function wanted: resolve relative path Skaperen 4 3,297 Sep-06-2018, 01:52 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

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