python如何下载bs4库_Python BS4库的安装与使用详解

Beautiful Soup 库一般被称为bs4库,支持Python3,是我们写爬虫非常好的第三方库。因用起来十分的简便流畅。所以也被人叫做“美味汤”。目前bs4库的最新版本是4.60。下文会介绍该库的最基本的使用,具体详细的细节还是要看:[官方文档](Beautiful Soup Documentation)

bs4库的安装

Python的强大之处就在于他作为一个开源的语言,有着许多的开发者为之开发第三方库,这样我们开发者在想要实现某一个功能的时候,只要专心实现特定的功能,其他细节与基础的部分都可以交给库来做。bs4库 就是我们写爬虫强有力的帮手。

安装的方式非常简单:我们用pip工具在命令行里进行安装 $ pip install beautifulsoup4

接着我们看一下是否成功安装了bs4库 $ pip list

这样我们就成功安装了 bs4 库

744a9bea1c781bc74684963cd198e148.png

bs4库的简单使用

这里我们先简单的讲解一下bs4库的使用,

暂时不去考虑如何从web上抓取网页,

假设我们需要爬取的html是如下这么一段:

下面的一段HTML代码将作为例子被多次用到.这是 爱丽丝梦游仙境的 的一段内容(以后内容中简称为 爱丽丝 的文档):

The Dormouse's story

The Dormouse's story

Once upon a time there were three little sisters; and their names were

http://example.com/elsie" class="sister" id="link1">Elsie,

http://example.com/lacie" class="sister" id="link2">Lacie and

http://example.com/tillie" class="sister" id="link3">Tillie;

and they lived at the bottom of a well.

...

下面我们开始用bs4库解析这一段html网页代码。 #导入bs4模块

from bs4 import BeautifulSoup

#做一个美味汤

soup = BeautifulSoup(html,'html.parser')

#输出结果

print(soup.prettify())

'''

OUT:

#

#

#

# The Dormouse's story

#

#

#

#

#

# The Dormouse's story

#

#

#

# Once upon a time there were three little sisters; and their names were

#

# Elsie

#

# ,

#

# Lacie

#

# and

#

# Tillie

#

# ; and they lived at the bottom of a well.

#

#

# ...

#

#

#

'''

可以看到bs4库将网页文件变成了一个soup的类型,

事实上,bs4库 是解析、遍历、维护、“标签树“的功能库。

通俗一点说就是: bs4库把html源代码重新进行了格式化,

从而方便我们对其中的节点、标签、属性等进行操作。

下面是几个简单的浏览结构化数据的方式 :

请仔细观察最前面的html文件 # 找到文档的title

soup.title

#

The Dormouse's story

#title的name值

soup.title.name

# u'title'

#title中的字符串String

soup.title.string

# u'The Dormouse's story'

#title的父亲节点的name属性

soup.title.parent.name

# u'head'

#文档的第一个找到的段落

soup.p

#

The Dormouse's story

#找到的p的class属性值

soup.p['class']

# u'title'

#找到a标签

soup.a

# http://example.com/elsie" id="link1">Elsie

#找到所有的a标签

soup.find_all('a')

# [http://example.com/elsie" id="link1">Elsie,

# http://example.com/lacie" id="link2">Lacie,

# http://example.com/tillie" id="link3">Tillie]

#找到id值等于3的a标签

soup.find(id="link3")

# http://example.com/tillie" id="link3">Tillie

通过上面的例子 我们知道bs4库是这样理解一个html源文件的:

首先 把html源文件转换为soup类型

接着 从中通过特定的方式抓取内容

更高级点的用法?

从文档中找到所有标签的链接: #发现了没有,find_all方法返回的是一个可以迭代的列表

for link in soup.find_all('a'):

print(link.get('href'))

# http://example.com/elsie

# http://example.com/lacie

# http://example.com/tillie

从文档中获取所有文字内容: #我们可以通过get_text 方法 快速得到源文件中的所有text内容。

print(soup.get_text())

# The Dormouse's story

#

# The Dormouse's story

#

# Once upon a time there were three little sisters; and their names were

# Elsie,

# Lacie and

# Tillie;

# and they lived at the bottom of a well.

#

# ...

bs4库的入门使用我们就先进行到这。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持聚米学院。

  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我知道你想了解如何用 Python 爬取新浪微博评论。以下是一些步骤供您参考: 1. 安装必要的使用 Python 爬取微博评论前,您需要安装一些必要的。其中,requests 和 BeautifulSoup 是必不可少的。 您可以使用以下命令在终端中安装它们: ``` pip install requests pip install beautifulsoup4 ``` 2. 登录微博并获取 cookie 在爬取微博评论之前,您需要登录您的微博账号并获取 cookie。 您可以使用 Chrome 浏览器登录您的微博账号。然后,按 F12 打开开发者工具,切换到“Network”选项卡并刷新页面。在“Filter”栏中输入“comment”,然后点击“XHR”选项卡。 接下来,您可以在“Headers”选项卡中找到“Request Headers”部分,复制“Cookie”字段的值。 3. 构造请求 URL 在获取 cookie 后,您需要构造请求 URL。以下是一个示例 URL:https://weibo.cn/comment/Jf3bhk1V5?uid=123456&page=1 其中,Jf3bhk1V5 是微博的 ID,123456 是用户的 ID,page 是评论的页数。 您可以使用 requests 发送 GET 请求并传递 cookie、微博 ID 和页数参数。 以下是一个示例代码: ``` import requests from bs4 import BeautifulSoup cookie = 'your_cookie_here' url = 'https://weibo.cn/comment/Jf3bhk1V5?uid=123456&page=1' headers = { 'Cookie': cookie } response = requests.get(url, headers=headers) soup = BeautifulSoup(response.text, 'html.parser') # 解析评论 ``` 4. 解析评论 最后,您需要解析页面上的评论。您可以使用 BeautifulSoup 来解析 HTML。 以下是一个示例代码: ``` comments = soup.find_all('div', class_='c') for comment in comments: # 获取评论内容 content = comment.find('span', class_='ctt').text # 获取评论时间 time = comment.find('span', class_='ct').text # 获取评论用户 user = comment.find('a', class_='nk').text # 输出评论信息 print(f'{user}({time}): {content}\n') ``` 以上就是用 Python 爬取新浪微博评论的一些步骤。请注意,爬取微博评论可能会违反微博的使用协议,因此请谨慎使用

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值