python爬虫的BeautifulSoup库详解(1)

...

“”"

from bs4 import BeautifulSoup

soup = BeautifulSoup(html, ‘lxml’)

print(soup.p.contents)

['\n Once upon a time there were three little sisters; and their names were\n ',

Elsie

, ‘\n’, Lacie, ’ \n and\n ', Tillie, '\n and they lived at the bottom of a well.\n ']

html = “”"

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.

...

“”"

from bs4 import BeautifulSoup

soup = BeautifulSoup(html, ‘lxml’)

print(soup.p.children)

for i, child in enumerate(soup.p.children):

print(i, child)

<list_iterator object at 0x1064f7dd8>

0

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

1

Elsie

2

3 Lacie

4

and

5 Tillie

6

and they lived at the bottom of a well.

html = “”"

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.

...

“”"

from bs4 import BeautifulSoup

soup = BeautifulSoup(html, ‘lxml’)

print(soup.p.descendants)

for i, child in enumerate(soup.p.descendants):

print(i, child)

<generator object descendants at 0x10650e678>

0

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

1

Elsie

2

3 Elsie

4 Elsie

5

6

7 Lacie

8 Lacie

9

and

10 Tillie

11 Tillie

12

and they lived at the bottom of a well.

3.7父节点和祖先节点

html = “”"

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.

...

“”"

from bs4 import BeautifulSoup

soup = BeautifulSoup(html, ‘lxml’)

print(soup.a.parent)

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.

html = “”"

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.

...

“”"

from bs4 import BeautifulSoup

soup = BeautifulSoup(html, ‘lxml’)

print(list(enumerate(soup.a.parents)))

[(0,

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.

), (1,

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.

...

), (2, 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.

...

), (3, 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.

...

)]

3.8兄弟节点

html = “”"

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.

...

“”"

from bs4 import BeautifulSoup

soup = BeautifulSoup(html, ‘lxml’)

print(list(enumerate(soup.a.next_siblings)))

print(list(enumerate(soup.a.previous_siblings)))

[(0, ‘\n’), (1, Lacie), (2, ’ \n and\n '), (3, Tillie), (4, '\n and they lived at the bottom of a well.\n ')]

[(0, '\n Once upon a time there were three little sisters; and their names were\n ')]

4标准选择器


4.1find_all( name , attrs , recursive , text , **kwargs )

可根据标签名、属性、内容查找文档

4.1.1name

html=‘’’

Hello
    • Foo
    • Bar
    • Jay
      • Foo
      • Bar
      • ‘’’

        from bs4 import BeautifulSoup

        soup = BeautifulSoup(html, ‘lxml’)

        print(soup.find_all(‘ul’))

        print(type(soup.find_all(‘ul’)[0]))

        [

        • Foo
        • Bar
        • Jay
        • ,
          • Foo
          • Bar
          • ]

            <class ‘bs4.element.Tag’>

            html=‘’’

            Hello
            • Foo
            • Bar
            • Jay
              • Foo
              • Bar
              • ‘’’

                from bs4 import BeautifulSoup

                soup = BeautifulSoup(html, ‘lxml’)

                for ul in soup.find_all(‘ul’):

                print(ul.find_all(‘li’))

                [

              • Foo
              • ,
              • Bar
              • ,
              • Jay
              • ]

                [

              • Foo
              • ,
              • Bar
              • ]

                4.1.2attrs

                html=‘’’

                Hello
                • Foo
                • Bar
                • Jay
                  • Foo
                  • Bar
                  • ‘’’

                    from bs4 import BeautifulSoup

                    soup = BeautifulSoup(html, ‘lxml’)

                    print(soup.find_all(attrs={‘id’: ‘list-1’}))

                    print(soup.find_all(attrs={‘name’: ‘elements’}))

                    [

                    • Foo
                    • Bar
                    • Jay
                    • ]

                      [

                      • Foo
                      • Bar
                      • Jay
                      • ]

                        html=‘’’

                        Hello
                        • Foo
                        • Bar
                        • Jay
                          • Foo
                          • Bar
                          • ‘’’

                            from bs4 import BeautifulSoup

                            soup = BeautifulSoup(html, ‘lxml’)

                            print(soup.find_all(id=‘list-1’))

                            print(soup.find_all(class_=‘element’))

                            [

                            • Foo
                            • Bar
                            • Jay
                            • ]

                              [

                            • Foo
                            • ,
                            • Bar
                            • ,
                            • Jay
                            • ,
                            • Foo
                            • ,
                            • Bar
                            • ]

                              4.1.3text

                              html=‘’’

                              Hello
                              • Foo
                              • Bar
                              • Jay
                                • Foo
                                • Bar
                                • ‘’’

                                  from bs4 import BeautifulSoup

                                  soup = BeautifulSoup(html, ‘lxml’)

                                  print(soup.find_all(text=‘Foo’))

                                  [‘Foo’, ‘Foo’]

                                  4.2find( name , attrs , recursive , text , **kwargs )

                                  find返回单个元素,find_all返回所有元素

                                  html=‘’’

                                  Hello
                                  • Foo
                                  • Bar
                                  • Jay
                                    • Foo
                                    • Bar
                                    • ‘’’

                                      from bs4 import BeautifulSoup

                                      soup = BeautifulSoup(html, ‘lxml’)

                                      print(soup.find(‘ul’))

                                      print(type(soup.find(‘ul’)))

                                      print(soup.find(‘page’))

                                      • Foo
                                      • Bar
                                      • Jay
                                      • <class ‘bs4.element.Tag’>

                                        None

                                        4.3find_parents() find_parent()

                                        find_parents()返回所有祖先节点,find_parent()返回直接父节点。

                                        4.4find_next_siblings() find_next_sibling()

                                        find_next_siblings()返回后面所有兄弟节点,find_next_sibling()返回后面第一个兄弟节点。

                                        4.5find_previous_siblings() find_previous_sibling()

                                        find_previous_siblings()返回前面所有兄弟节点,find_previous_sibling()返回前面第一个兄弟节点。

                                        4.6find_all_next() find_next()

                                        find_all_next()返回节点后所有符合条件的节点, find_next()返回第一个符合条件的节点

                                        4.7find_all_previous() 和 find_previous()

                                        find_all_previous()返回节点后所有符合条件的节点, find_previous()返回第一个符合条件的节点

                                        5.CSS选择器


                                        通过select()直接传入CSS选择器即可完成选择

                                        html=‘’’

                                        Hello
                                        • Foo
                                        • Bar
                                        • Jay
                                          • Foo
                                          • Bar
                                          • ‘’’

                                            from bs4 import BeautifulSoup

                                            soup = BeautifulSoup(html, ‘lxml’)

                                            print(soup.select(‘.panel .panel-heading’))

                                            print(soup.select(‘ul li’))

                                            print(soup.select(‘#list-2 .element’))

                                            print(type(soup.select(‘ul’)[0]))

                                            [

                                            Hello
                                            ]

                                            [

                                          • Foo
                                          • ,
                                          • Bar
                                          • ,
                                          • Jay
                                          • ,
                                          • Foo
                                          • ,
                                          • Bar
                                          • ]

                                            [

                                          • Foo
                                          • ,
                                          • Bar
                                          • ]

                                            <class ‘bs4.element.Tag’>

                                            html=‘’’

                                            自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

                                            深知大多数Python工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

                                            因此收集整理了一份《2024年Python开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
                                            img
                                            img



                                            既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Python开发知识点,真正体系化!

                                            由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新

                                            如果你觉得这些内容对你有帮助,可以添加V获取:vip1024c (备注Python)
                                            img

                                            做了那么多年开发,自学了很多门编程语言,我很明白学习资源对于学一门新语言的重要性,这些年也收藏了不少的Python干货,对我来说这些东西确实已经用不到了,但对于准备自学Python的人来说,或许它就是一个宝藏,可以给你省去很多的时间和精力。

                                            别在网上瞎学了,我最近也做了一些资源的更新,只要你是我的粉丝,这期福利你都可拿走。

                                            我先来介绍一下这些东西怎么用,文末抱走。


                                            (1)Python所有方向的学习路线(新版)

                                            这是我花了几天的时间去把Python所有方向的技术点做的整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照上面的知识点去找对应的学习资源,保证自己学得较为全面。

                                            最近我才对这些路线做了一下新的更新,知识体系更全面了。

                                            在这里插入图片描述

                                            (2)Python学习视频

                                            包含了Python入门、爬虫、数据分析和web开发的学习视频,总共100多个,虽然没有那么全面,但是对于入门来说是没问题的,学完这些之后,你可以按照我上面的学习路线去网上找其他的知识资源进行进阶。

                                            在这里插入图片描述

                                            (3)100多个练手项目

                                            我们在看视频学习的时候,不能光动眼动脑不动手,比较科学的学习方法是在理解之后运用它们,这时候练手项目就很适合了,只是里面的项目比较多,水平也是参差不齐,大家可以挑自己能做的项目去练练。

                                            在这里插入图片描述

                                            (4)200多本电子书

                                            这些年我也收藏了很多电子书,大概200多本,有时候带实体书不方便的话,我就会去打开电子书看看,书籍可不一定比视频教程差,尤其是权威的技术书籍。

                                            基本上主流的和经典的都有,这里我就不放图了,版权问题,个人看看是没有问题的。

                                            (5)Python知识点汇总

                                            知识点汇总有点像学习路线,但与学习路线不同的点就在于,知识点汇总更为细致,里面包含了对具体知识点的简单说明,而我们的学习路线则更为抽象和简单,只是为了方便大家只是某个领域你应该学习哪些技术栈。

                                            在这里插入图片描述

                                            (6)其他资料

                                            还有其他的一些东西,比如说我自己出的Python入门图文类教程,没有电脑的时候用手机也可以学习知识,学会了理论之后再去敲代码实践验证,还有Python中文版的库资料、MySQL和HTML标签大全等等,这些都是可以送给粉丝们的东西。

                                            在这里插入图片描述

                                            这些都不是什么非常值钱的东西,但对于没有资源或者资源不是很好的学习者来说确实很不错,你要是用得到的话都可以直接抱走,关注过我的人都知道,这些都是可以拿到的。

                                            在这里插入图片描述

                                            (2)Python学习视频

                                            包含了Python入门、爬虫、数据分析和web开发的学习视频,总共100多个,虽然没有那么全面,但是对于入门来说是没问题的,学完这些之后,你可以按照我上面的学习路线去网上找其他的知识资源进行进阶。

                                            在这里插入图片描述

                                            (3)100多个练手项目

                                            我们在看视频学习的时候,不能光动眼动脑不动手,比较科学的学习方法是在理解之后运用它们,这时候练手项目就很适合了,只是里面的项目比较多,水平也是参差不齐,大家可以挑自己能做的项目去练练。

                                            在这里插入图片描述

                                            (4)200多本电子书

                                            这些年我也收藏了很多电子书,大概200多本,有时候带实体书不方便的话,我就会去打开电子书看看,书籍可不一定比视频教程差,尤其是权威的技术书籍。

                                            基本上主流的和经典的都有,这里我就不放图了,版权问题,个人看看是没有问题的。

                                            (5)Python知识点汇总

                                            知识点汇总有点像学习路线,但与学习路线不同的点就在于,知识点汇总更为细致,里面包含了对具体知识点的简单说明,而我们的学习路线则更为抽象和简单,只是为了方便大家只是某个领域你应该学习哪些技术栈。

                                            在这里插入图片描述

                                            (6)其他资料

                                            还有其他的一些东西,比如说我自己出的Python入门图文类教程,没有电脑的时候用手机也可以学习知识,学会了理论之后再去敲代码实践验证,还有Python中文版的库资料、MySQL和HTML标签大全等等,这些都是可以送给粉丝们的东西。

                                            在这里插入图片描述

                                            这些都不是什么非常值钱的东西,但对于没有资源或者资源不是很好的学习者来说确实很不错,你要是用得到的话都可以直接抱走,关注过我的人都知道,这些都是可以拿到的。

                                          • 13
                                            点赞
                                          • 30
                                            收藏
                                            觉得还不错? 一键收藏
                                          • 0
                                            评论

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

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

                                          请填写红包祝福语或标题

                                          红包个数最小为10个

                                          红包金额最低5元

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

                                          抵扣说明:

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

                                          余额充值