python三方库之BeautifuSoup

html文档解析的三方库beautifulsoup4

什么是beautifulsoup?

学习资源:https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html

1.安装

pip install beautifulsoup4

2.使用

至少要对html有一定的了解。

from bs4 import BeautifulSoup

举例:获取一个页面中的所有链接

def get_link(url="http://www.zhihu.com"):
    hrefs = []
    html = urllib2.urlopen(url=url).read()
    soup = BeautifulSoup(html, "html.parser")
    for link in soup.find_all('a'):
        href = link.get('href')
        if not href.startswith('http'):
            href = url + href
        hrefs.append(href)
    return hrefs



posted @   叶常落  阅读(43)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
点击右上角即可分享
微信分享提示