Python练习册,每天一个小程序(十)

  • 时间:
  • 来源:互联网
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/oShiShuiNianHua1234/article/details/103344807

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

解答,这个问题的解答同上一题解答方式,使用soup获取所有的href链接

#encoding:utf-8

import requests
from bs4 import BeautifulSoup
import urllib.request

def get_page(url):
    response = requests.get(url)
    soup = BeautifulSoup(response.text, "lxml")
    return soup

def get_page_urllib(url):
    resp = urllib.request.urlopen(url)
    soup = BeautifulSoup(resp.read(), "html.parser")
    return soup

def get_page_links(soup):
    links = []
    page_links = soup.find_all("link")
    for link in page_links:
        links.append(link.get("href"))
    return links

soup = get_page("https://github.com/Yixiaohan/show-me-the-code")
links = get_page_links(soup)
print(links)

 

本文链接http://element-ui.cn/news/show-478.aspx