練習008-009

第 0008 題:一個HTML文件,找出裏面的正文。
第 0009 題:一個HTML文件,找出裏面的鏈接。

使用的BeautifulSoup來完成的,只需要調用方法就可以,比較方便
程序如下:

#!/usr/bin python 
#coding:utf-8
from bs4 import BeautifulSoup
html='''
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><b>The Dormouse's story</b></p>

<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>

<p class="story">...</p>
'''

soup = BeautifulSoup(html)

print soup.get_text()

for i in soup.findAll('a'):
    print i.get('href')

感興趣的可以看看這個文檔
BeautifulSoup4.2.0文檔

( 寫於2016年5月6日,http://blog.csdn.net/bzd_111

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章