python 學習之FAQ:find 與 find_all 使用

來源:互聯網
上載者:User

標籤:parse   學習   注意   resultset   find   tee   內容   bsp   beautiful   

 

FAQ記錄

 1. 錯誤源碼

錯誤源碼如下

    def fillUnivList(_html,_ulist):    soup =BeautifulSoup(_html,‘html.parser‘)    for tr in soup.find_all(‘tbody‘).children:    if isinstance(tr,bs4.element.Tag):    tds = tr.find_all(‘td‘)    _ulist.append((tds[0].string,tds[1].string,tds[3].string))

 

2. 報錯顯示

運行報錯顯示

1     File"printUnivLst.py", line 26,in getUnivList2     for tr in soup.find_all(‘tbody‘).children:#注意find與find_all不同使用情境3     AttributeError:‘ResultSet‘ object has no attribute ‘children‘

3. 報錯分析

  報錯內容AttributeError: ‘ResultSet‘ object has no attribute ‘children‘意思為屬性錯誤:結果集對象每一屬性可以調用。

  分析錯誤源碼既可以知道soup對象的方法find_all()返回的是一個結果集列表,而結果集列表是一組資料,其是沒有屬性的,只有單個資料對象才有屬性可言。

  故,原錯誤源碼中,對於tbody標籤的子標籤內容進行尋找時,應該使用find().children,而soup.find()返回的結果一個字串對象,其可以存在屬性的調用。

4. 修改的代碼
1     # 大學排名資料提取,並存入列表2     def getUnivList(_html,_Univlst):3     soup =BeautifulSoup(_html,‘html.parser‘)4     for tr in soup.find(‘tbody‘).children:#注意find 與find_all不同使用情境5     if isinstance(tr,bs4.element.Tag):6     tds = tr.find_all(‘td‘)7     _Univlst.append((tds[0].string,tds[1].string,tds[3].string))

 

python 學習之FAQ:find 與 find_all 使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.