AttributeError: 'NavigableString' object has no attribute 'find_all' (NameError)

10,254

If you are attempting to find the text associated with the dt with the class attribute articleSubject, you can directly find it within the item.

By doing this:

>>> for item in a_data:
...     print item.find_all("dt",{"class":"articleSubject"})[0].text

This prints:

**시총 9조 `모바일 공룡` 다음카카오 합병 승인…업계 지각변동 예고**

for the above HTML. If you run this code against the URL in your code, you will get 20 results.

item.contents is a list with \n as the first item. So doing a find_all() on a new line character throws the AttributeError

Share:
10,254
appleJuice
Author by

appleJuice

Updated on June 04, 2022

Comments

  • appleJuice
    appleJuice almost 2 years
    import requests
    from bs4 import BeautifulSoup
    url=("http://finance.naver.com/news/mainnews.nhn")
    
    r=requests.get(url)
    soup=BeautifulSoup(r.content)
    
    a_data = soup.find_all("li",{"class":"block1"})
    
    for item in a_data:
        print item.contents[0].find_all("dt",{"class":"articleSubject"})[0].text
    

    In this code, when i run this code. I have error like this. "AttributeError: 'NavigableString' object has no attribute 'find_all'"

    How can i solve this problem ???? I have already tried try and except method. However, it didn't work...

    // Html code

    <li class="block1">
    <dl>
    <dt class="articleSubject">
    <span class="remark"></span> <!-- 말머리는 span class="remark" 로 묶임 -->
    <a href="/news/news_read.nhn?       
    article_id=0003289339&amp;office_id=009&amp;mode=mainnews&amp;type=&amp;date=2014-08-   
    27&amp;page=1">**시총 9조 `모바일 공룡` 다음카카오 합병 승인…업계 지각변동 예고**</a>
    </dt>
    <dd class="articleSummary">
    다음카카오가 합병을 위한 마지막 문턱을 넘어섰다. 네이버의 독주가 지속되고 있는 온라인·모바일 업계에    
    적지 않은 시장 판도 변화가 예상된다.   27일 다음과 카카오는 각각 제주 ..
                                        <span class="press">매일경제 </span>
    <span class="bar">|</span>
    <span class="wdate">2014-08-27 11:40:05</span>
    </dd>
    </dl>
    </li>