skip to Main Content

I make script for shopify cart since it is impossible to purchase manually, when I ran the script to my command prompt,
it says ,

line 109, in AttributeError: "str’ object has no attribute
‘text

Scrape Product Info based on selected colors

        if blue and cinder:
            productInfo(urlBlueResponse.text)
            productInfo(urlCinderResponse.text)
        elif blue:
            productInfo(urlBlueResponse.text)
        elif cinder:
            productInfo(urlCinderResponse.text)
        else:
            print(Fore.RED + timestamp

I was told it was from a capitalization mismatch, can somebody please explain this to me. I am new to coding and I want to learn all I can.

3

Answers


  1. This error happened when you tried to access an attribute on a string object — .text — that does not exist as an element on that object.

    It looks like your code is working with HTTP request and response objects of some kind: urlBlueResponse

    It is plausible that you got an error or some other unexpected behavior in the request/response cycle that resulted in one of the response objects returning a str (string) type instead of a response object with a text attribute. I suggest you handle the exception with a try/except block:

    try:    
        if blue and cinder:
            productInfo(urlBlueResponse.text)
            productInfo(urlCinderResponse.text)
        elif blue:
            productInfo(urlBlueResponse.text)
        elif cinder:
            productInfo(urlCinderResponse.text)
        else:
            print(Fore.RED + timestamp)
    except AttributeError as e:
        #exception handler logic goes here
        print("got exception: ")
        print(e)
        #if error indicates your request is recoverable then do so:
        if recoverable(e):
            do_request(again)
        #if error is unrecoverable, decorate it and reraise it
        #(See *Link)
        # or just reraise it:
            raise(e)   
    

    *Link: (Re-raise exception with a different type and message, preserving existing information)

    Login or Signup to reply.
  2. Based on the error message, either urlBlueResponse or urlCinderResponse (or both) are a string datatype. The way you are using them, it appears you expect these to be objects which have a text attribute. The error message is telling you that they’re str objects and don’t have text attributes.

    Login or Signup to reply.
  3. urlBlueResponse = eval(urlBlueResponse)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search