2017-01-12 9 views
-2

私はpython newbieです。私は学校のサイトからデータを取得しようとしていました。以下は、ニュース項目のみをスクラップするために書いたコードです。それは動作しますが、タイトル、日付、段落を改行したいと思います。私のコードには何かがないと感じていますが、私はそれにハングアップはありません。あなたの助けが必要です。これらのHTMLタグのデータはどうやって区切りますか?

from bs4 import BeautifulSoup 
from urllib.request import urlopen 


page = urlopen("http://www.kibabiiuniversity.ac.ke") 
soup = BeautifulSoup(page) 

for i in soup.findAll("div", {"class": "blog-thumbnail-inside"}): 
    print (i.get_text()) 
    print ("----------" *20) 

ここでは、私が掻き取ろうとしているページのhtmlタグ構造を示します。

<div class="blog-thumbnail-inside"> 
    <h2 class="blog-thumbnail-title post-widget-title-color gdl-title"> 
     <a href="http://www.kibabiiuniversity.ac.ke"> 
      Completion of fees & collection of exam cards. 
     </a> 
    </h2> 
    <div class="blog-thumbnail-info post-widget-info-color gdl-divider"> 
     <div class="blog-thumbnail-date">Posted on 09 Jan 2017</div> 
    </div> 
    <div class="blog-thumbnail-context"> 
     <div class="blog-thumbnail-content"> 
      Download the information on fee payment and collection of exam cards.. 
     </div> 
    </div> 
</div> 

答えて

0
for i in soup.findAll("div", {"class": "blog-thumbnail-inside"}): 
    print (i.get_text('\n')) #You can specify a string to be used to join the bits of text together 
    print ("----------" *20) 

アウト:

Final Undergraduate Examination Timetable for Semester 1 2016/2017 
Posted on 11 Jan 2017 
Download Undergraduate Timetable 
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
Vacancies for Administrative and Teaching Positions 
Posted on 11 Jan 2017 
Kibabii University is a fully fledged public institution of higher education and research in Kenya with a student population of 6400 and staff population of 346. The University seeks to appoint innovative individuals with experience and excellent credentials 
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
+1

おかげで、それは私が行方不明になったビットです。 – kenda

関連する問題