您的位置:首页 > 编程语言 > Python开发

python三方库之BeautifuSoup

2017-03-28 22:22 260 查看

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: