hhh终于学完了python的入门课程,了解了python的基本语法,感觉好像会编写python的程序,于是又接触了python爬虫。嗯~ o( ̄▽ ̄)o猛然发现我tm
刚接触爬虫的大门,我差点直接裂开,为什么一个
BeautifulSoup
库如此难搞,当我从早上八点开始直到下午六点,笔记本密密麻麻抄了八九页,我突然发现此库包含内容甚多,并非一下子就能搞懂,终于明白一句话“人心不足蛇吞象”
冷静之后,对于和我一样刚入门的小白我分享一下我的经验,当然这不是正文,可以直接选择跳过:
对于刚入门的朋友,我想说的是爬虫的一个基本库往往内容包含的非常多,如果你只根据资料学习有关库的所有知识,我认为这是浪费时间,我建议可以将库稍作了解,不必死抠牛角尖,而是阅读大量的代码(你可以打开中国大学mooc,B站等搜索python爬虫课程,然后看它刚开始举的一些例子,逐行了解代码,通过百度搜索,了解具体的一个函数或者一个方法的具体用法)慢慢积累,通过这样,你就可以慢慢体会库的所有内容。··当然个人的拙见,不必过于当真。对对对,赶紧进入正题
在python的第三方库中BeautifulSoup
库的主要功能是从网页爬取数据,这是官方解释
Beautiful Soup is a Python library for pulling data out of HTML and XML files. It works with your favorite parser to provide idiomatic ways of navigating, searching, and modifying the parse tree. It commonly saves programmers hours or days of work.
至于BeautifulSoup
的安装也非常简单,打开命令窗口pip
Beautiful Soup
将复杂HTML文档转换成一个复杂的树形结构,每个节点都是Python对象,所有对象可以归纳为4种:Tag
NavigableString
BeautifulSoup
Comment
本文主要浅析这四类的含义
----------------这是一串网页的源代码,作为本篇文章的一个引例(文章来自官网,有省略)-------------------
html_doc = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><b>The Dormouse's story</b></p>
<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<p class="story">...</p>
"""
Tag
什么是Tag
按我个人的理解,就如它的字面意思,它就是标签。上面的 title,a,p 等等 HTML 标签加上里面包括的内容就是Tag, BeautifulSoup
可以简单的来获取Tag
print soup.title #后的为生成的内容
#<title>The Dormouse's story</title>
Tag具有很多的属性和方法,而其中最重要的属性就是name和attributes,每个tag都有自己的名字,通过 .name 来获取:
print soup.name
print soup.head.name
#[document]
#head
.attr就可以获得Tag的的一些属性,并且表达成字典的形式
print soup.p.attrs
#{'class': ['title']}
可以通过以下代码验证
print soup.p['class']
#['title']
You can add, remove, and modify a tag’s attributes. Again, this is
done by treating the tag as a dictionary:
tag的属性可以被添加,删除或修改
tag['class'] = 'verybold'
tag['id'] = 1
tag
# <blockquote class="verybold" id="1">Extremely bold</blockquote>
del tag['class']
del tag['id']
tag
# <blockquote>Extremely bold</blockquote>
tag['class']
# KeyError: 'class'
print(tag.get('class'))
# None
NavigableString
字符串常被包含在tag内.Beautiful Soup用 NavigableString 类来包装在tag中的字符串,因此NavigableString就是获取标签里面的内容。通过.string
print soup.p.string
#The Dormouse's story
它的类型为
print type(soup.p.string)
#<class 'bs4.element.NavigableString'>
因此只要判断类型满足'bs4.element.NavigableString'
就可以通过.string
输出里面的文本
A NavigableString is just like a Python Unicode string, except that it also supports some of the features described in Navigating the tree and Searching the tree. You can convert a NavigableString to a Unicode string with unicode() (in Python 2) or str (in Python 3)
个人的观点来看NavigableString就是从每一点入手,就如其字面——可遍历的字符串
BeautifulSoup
BeautifulSoup 对象表示的是一个文档的全部内容.大部分时候,可以把它当作 Tag 对象
Since the BeautifulSoup object doesn’t correspond to an actual HTML or XML tag, it has no name and no attributes. But sometimes it’s useful to look at its .name, so it’s been given the special .name “[document]”:
又因为 BeautifulSoup 对象并不是真正的HTML或XML的tag,所以它没有name和attribute属性.但有时查看它的 .name 属性是很方便的,所以 BeautifulSoup 对象包含了一个值为 “[document]” 的特殊属性 .name
print type(soup.name)
#<type 'unicode'>
print soup.name
# [document]
print soup.attrs
#{} 空字典
所以个人理解为BeautifulSoup就是从整个代码的角度来理解bs4
Comment
Comment 对象是一个特殊类型的 NavigableString 对象,其实输出的内容仍然不包括注释符号
The Comment object is just a special type of NavigableString,But when it appears as part of an HTML document, a Comment is displayed with special formatting
例如有这样一段代码,
markup = "<b><!--Hey, buddy. Want to give me a hug?--></b>"
soup = BeautifulSoup(markup)
comment = soup.b.string
当查看它的类型是就会发现
type(comment)
# <class 'bs4.element.Comment'>
因此当它出现在HTML文档中时, Comment 对象会使用特殊的格式输出:
print(soup.b.prettify())
# <b>
# <!--Hey, buddy. Want to give me a hug?-->
# </b>
故我认为comment就是特殊字符的处理
|
最后总结beautifulsoup
的四大对象种类
tag:标签
NavigableString:包装在tag中的字符串
BeautifulSoup:这个文档的全部内容
comment:特殊类型
这就是我这位爬虫小白对beautifulsoup
库的一些非常肤浅的理解,还望各位大佬不吝赐教,提前祝大家新年快乐。