Reputation: 137
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&office_id=009&mode=mainnews&type=&date=2014-08-
27&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>
Upvotes: 2
Views: 10474
Reputation: 380
I would separate in your loop NavigableStrings from tags. First:
import NavigableString
then in your loop test all the elements in the soup to see if they are tags or NavigableStrings by using:
if isinstance(object, NavigableString):
Do something with this event
else:
Do something with thing that is not NavigableString
Upvotes: 0
Reputation: 12092
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
Upvotes: 1