Python 练习册 0007、0008题 (网页分析)

第 0008 题:一个HTML文件,找出里面的正文。

第 0009 题:一个HTML文件,找出里面的链接。

import requests
from bs4 import BeautifulSoup


def get_content(page_url):
    web_data = requests.get(page_url)
    soup = BeautifulSoup(web_data.content, 'lxml')
    contents = soup.select('div.show-content')

    result = ''
    for content in contents:
        result += content.get_text()
    print(result)
    return result


def get_href(page_url):
    web_data = requests.get(page_url)
    soup = BeautifulSoup(web_data.content, 'lxml')
    a_tags = soup.find_all('a')

    href_list = []
    for a_tag in a_tags:
        href = a_tag.get('href')
        if href:
            print(a_tag.get('href'))
            href_list.append(href)
    print(href_list)
    return href_list


page_url = 'http://www.jianshu.com/p/40fc848414ea'
get_href(page_url=page_url)

你可能感兴趣的:(Python 练习册 0007、0008题 (网页分析))