简体   繁体   English

AttributeError:“str”对象没有属性“文本”

[英]AttributeError: "str' object has no attribute 'text

I make script for shopify cart since it is impossible to purchase manually, when I ran the script to my command prompt, it says ,我为shopify购物车制作脚本,因为无法手动购买,当我将脚本运行到命令提示符时,它说,

line 109, in AttributeError: "str' object has no attribute 'text第 109 行,在 AttributeError 中:“str”对象没有属性“文本”

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.我是编码新手,我想尽我所能学习。

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.当您尝试访问不作为该对象上的元素存在的字符串对象 -- .text -- 上的属性时,会发生此错误。

It looks like your code is working with HTTP request and response objects of some kind: urlBlueResponse看起来您的代码正在处理某种 HTTP 请求和响应对象: 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.您可能在请求/响应周期中遇到错误或其他一些意外行为,导致其中一个响应对象返回str (字符串)类型,而不是具有文本属性的响应对象。 I suggest you handle the exception with a try/except block:我建议您使用 try/except 块处理异常:

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 ) *链接:( 使用不同的类型和消息重新引发异常,保留现有信息

Based on the error message, either urlBlueResponse or urlCinderResponse (or both) are a string datatype.根据错误消息, urlBlueResponseurlCinderResponse (或两者)都是字符串数据类型。 The way you are using them, it appears you expect these to be objects which have a text attribute.您使用它们的方式,您似乎希望它们是具有text属性的对象。 The error message is telling you that they're str objects and don't have text attributes.错误消息告诉您它们是str对象并且没有text属性。

urlBlueResponse = eval(urlBlueResponse)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM