亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在亞馬遜網絡抓取時在BS4中收到錯誤:屬性錯誤:“NoneType”對象沒有屬性“get_text”

在亞馬遜網絡抓取時在BS4中收到錯誤:屬性錯誤:“NoneType”對象沒有屬性“get_text”

慕森王 2022-08-02 10:52:19
!pip install requests!pip install bs4import requestsfrom bs4 import BeautifulSoupurl = "https://www.amazon.in/Apple-iPhone-Pro-Max-256GB/dp/B07XVLH744/ref=sr_1_1_sspa?crid=2VCKZNOH3H6SR&keywords=apple+iphone+11+pro+max&qid=1582043410&sprefix=apple+iphone%2Caps%2C388&sr=8-1-spons&psc=1&spLa=ZW5jcnlwdGVkUXVhbGlmaWVyPUEyVjdZSE83TzU4UUMmZW5jcnlwdGVkSWQ9QTAyNTI1ODZJUzZOVUwxWDNIUlAmZW5jcnlwdGVkQWRJZD1BMDkxNDg4MzFLMFpVT1M5OFM5Q0smd2lkZ2V0TmFtZT1zcF9hdGYmYWN0aW9uPWNsaWNrUmVkaXJlY3QmZG9Ob3RMb2dDbGljaz10cnVl"headers = {"User-Agent": "in this section im adding my user agent after typing my user agent in google search"}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()converted_price = price[0:8]print(converted_price)print(titles)我正在谷歌colab上工作,當我運行這個代碼時,我得到這個錯誤AttributeError   Traceback (most recent call last)<ipython-input-15-14696d9dc778> in <module>()     16 print(soup.prettify())     17 ---> 18 title = soup.find(id = "productTitle").get_text()     19 price = soup.find(id = "priceblock_ourprice").get_text()     20 AttributeError: 'NoneType' object has no attribute 'get_text'我嘗試在互聯網上搜索,但沒有找到解決我問題的答案。我試圖得到iPhone 11專業最高價格。當我運行此代碼時,我得到上面提到的錯誤。
查看完整描述

4 回答

?
白衣非少年

TA貢獻1155條經驗 獲得超0個贊

soup.find(id = "productTitle")這是返回,因為它無法找到.確保搜索正確的元素。Noneid = "producTitle"


對于語句,我建議始終編寫if條件以避免和處理此類錯誤。find


title = soup.find(id = "productTitle")

if title:

    title = title.get_text()

else:

    title = "default_title"


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

您可以使用 執行相同的操作。price


查看完整回答
反對 回復 2022-08-02
?
交互式愛情

TA貢獻1712條經驗 獲得超3個贊

好吧,我在這里測試了你的代碼,它工作正常。但是,當您嘗試在短時間內訪問同一鏈接時,亞馬遜會為您提供503代碼...


<html>

 <head>

  <title>

   503 - Service Unavailable Error

  </title>

 </head>

 <body bgcolor="#FFFFFF" text="#000000">

  <!--

        To discuss automated access to Amazon data please contact [email protected].

        For information about migrating to our APIs refer to our Marketplace APIs at https://developer.amazonservices.in/ref=rm_5_sv, or our Product Advertising API at https://affiliate-program.amazon.in/gp/advertising/api/detail/main.html/ref=rm_5_ac for advertising use cases.

-->

  <center>

   <a href="https://www.amazon.in/ref=cs_503_logo/">

    <img alt="Amazon.in" border="0" height="45" src="https://images-eu.ssl-images-amazon.com/images/G/31/x-locale/communities/people/logo.gif" width="200"/>

   </a>

   <p align="center">

    <font face="Verdana,Arial,Helvetica">

     <font color="#CC6600" size="+2">

      <b>

       Oops!

      </b>

     </font>

     <br/>

     <b>

      It's rush hour and traffic is piling up on that page. Please try again in a short while.

      <br/>

      If you were trying to place an order, it will not have been processed at this time.

     </b>

     <p>

      <img alt="*" border="0" height="9" src="https://images-eu.ssl-images-amazon.com/images/G/02/x-locale/common/orange-arrow.gif" width="10"/>

      <b>

       <a href="https://www.amazon.in/ref=cs_503_link/">

        Go to the Amazon.in home page to continue shopping

       </a>

      </b>

     </p>

    </font>

   </p>

  </center>

 </body>

</html>

請稍等片刻,然后您可以重試,或者至少在請求之間測試更長的時間...


查看完整回答
反對 回復 2022-08-02
?
慕蓋茨4494581

TA貢獻1850條經驗 獲得超11個贊

當您嘗試從值為 None 的對象中提取數據時,您會收到該錯誤。如果您在第 18 行看到這一點,則表示您不匹配任何內容并返回了 None。soup.find(id = "productTitle")


您需要將處理分解為多個步驟。在訪問返回值之前,請先檢查它。所以。。。


title_info = soup.find(id = "productTitle")

if title_info:

    title = title_info.text

else:

    'handle the situation'


查看完整回答
反對 回復 2022-08-02
?
繁星淼淼

TA貢獻1775條經驗 獲得超11個贊

也試試這個代碼


    title = soup.find(id="productTitle")

     if title:

       title = title.get_text()

     else:

       title = "default_title"

    price = soup.find(id="priceblock_ourprice")

      if price:

       price = price

      else:

       price = "default_title"


        # converted_price = price[0:8]

       convert = str(price)

       con = convert[-18:-11]


        print(con)

        print(title)

嘗試使用其他 IDE


使用 repl.it= https://repl.it 創建新的 repl 并使用它


查看完整回答
反對 回復 2022-08-02
  • 4 回答
  • 0 關注
  • 284 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號