Python每日練習 07 一個HTML文件,找出裏面的正文與鏈接

#一個HTML文件,找出裏面的正文與鏈接
import requests
from bs4 import BeautifulSoup
def search_body_urls(path):
    #path = 'http://mil.news.sina.com.cn/china/2017-04-05/doc-ifycwymx3854291.shtml'
    page = requests.get(path)
    page.encoding = 'utf-8'
    soup = BeautifulSoup(str(page.text),'html.parser')
    article = soup.select('.content')[0].text
    urls = soup.findAll('a')
    for u in urls:
         print(u['href'])
    print(article)

if __name__ == '__main__':
    search_body_urls(path='http://mil.news.sina.com.cn/china/2017-04-05/doc-ifycwymx3854291.shtml')
截圖如下:

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