美文网首页大数据 爬虫Python AI SqlPython小哥哥
Python爬虫:有了 BeautifulSoup ,再也不用担

Python爬虫:有了 BeautifulSoup ,再也不用担

作者: 轻松学Python111 | 来源:发表于2019-03-29 21:25 被阅读4次
Python爬虫:有了 BeautifulSoup ,再也不用担心我的正则表达式了

有些朋友觉得利用正则表达式去提取信息

太特么麻烦了

Python爬虫:有了 BeautifulSoup ,再也不用担心我的正则表达式了

有没有什么别的方式

更方便过滤我们想要的内容啊

emmmm

你还别说

还真有

有一个高效的网页解析库

它的名字叫做

BeautifulSoup

那可是

Python爬虫:有了 BeautifulSoup ,再也不用担心我的正则表达式了

是一个可以从 HTML 或 XML 文件中提取数据的 Python 库

那么这么玩呢

...

接下来就是

学习python的正确姿势

Python爬虫:有了 BeautifulSoup ,再也不用担心我的正则表达式了

首先我们要安装一下这个库

pip install beautifulsoup4

beautifulsoup支持不同的解析器

比如

对 HTML 的解析

对 XML 的解析

对 HTML5 的解析

你看

Python爬虫:有了 BeautifulSoup ,再也不用担心我的正则表达式了

一般情况下

我们用的比较多的是 lxml 解析器

我们先来使用一个例子

让你体验一下

beautifulsoup 的一些常用的方法

可流弊了呢

Python爬虫:有了 BeautifulSoup ,再也不用担心我的正则表达式了

比如我们有这样一段 HTML 代码

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;">html_doc = """
<html><head><title>学习python的正确姿势</title></head>
<body>
<p class="title"><b>小帅b的故事</b></p>
<p class="story">有一天,小帅b想给大家讲两个笑话
<a href="http://example.com/1" class="sister" id="link1">一个笑话长</a>,
<a href="http://example.com/2" class="sister" id="link2">一个笑话短</a> ,
他问大家,想听长的还是短的?</p>
<p class="story">...</p>
"""
</pre>

在不使用 re 来进行正则表达式的情况下

如何快速获取到我们想要的内容呢?

先安装一下

pip install beautifulsoup4

pip install lxml

接着将 html 的源代码传给 BeautifulSoup

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;">soup = BeautifulSoup(html_doc,'lxml')
</pre>

此时此刻

就不需要自己写正则匹配了

Python爬虫:有了 BeautifulSoup ,再也不用担心我的正则表达式了

我们要做的就是从这个对象直接获取我们要的内容

获取标题的内容

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;">print(soup.title.string)

学习python的正确姿势

</pre>

获取 p 标签里面的内容

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;">print(soup.p.string)

小帅b的故事

</pre>

获取 title 的父级标签

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;">print(soup.title.parent.name)

head

</pre>

获取超链接

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;">print(soup.a)

<a class="sister" href="http://example.com/1" id="link1">一个笑话长</a>

</pre>

获取所有超链接

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;">print(soup.find_all('a'))

[<a class="sister" href="http://example.com/1" id="link1">一个笑话长</a>, <a class="sister" href="http://example.com/2" id="link2">一个笑话短</a>]

</pre>

获取 id 为 link2 的超链接

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;">print(soup.find(id="link2"))

<a class="sister" href="http://example.com/2" id="link2">一个笑话短</a>

</pre>

获取网页中所有的内容

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;">print(soup.get_text())

学习python的正确姿势

小帅b的故事
有一天,小帅b想给大家讲两个笑话
一个笑话长,
一个笑话短 ,
他问大家,想听长的还是短的?
...
</pre>

除了find方法之外

如果你对css比较熟悉

也可以使用 select 方法

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;">soup = BeautifulSoup(html_doc,'lxml')
print(soup.select("title"))
print(soup.select("body a"))
print(soup.select("p > #link1"))
</pre>

以上就是 BeautifulSoup 常用的方法

想进一步了解可以到这

https://www.crummy.com/software/BeautifulSoup/bs4/doc/

有了它

妈妈再也不用担心我的正则表达式了

下次还有人这样问你

年轻人,不会正则表达式你睡得着觉?有点出息没有?

你可以傲娇的告诉他

Python爬虫:有了 BeautifulSoup ,再也不用担心我的正则表达式了

睡得着

相关文章

网友评论

    本文标题:Python爬虫:有了 BeautifulSoup ,再也不用担

    本文链接:https://www.haomeiwen.com/subject/qazrbqtx.html