ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

轻轻学爬虫—scrapy框架巧用7—猴子偷桃(3)

2021-06-30 20:56:34  阅读:187  来源: 互联网

标签:story 偷桃 parent 爬虫 Dormouse soup scrapy html print


# 轻轻学爬虫—scrapy框架巧用7—猴子偷桃(3) 上节课我们讲解了bs4的一部分使用方法,今天我们来继续学习。我们还是以上节课的数据为例子 ```python html_doc = """ The Dormouse's story <body>

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_doc, 'html.parser') soup.prettify() print(soup) #得到下面结构化的html ""“ The Dormouse's story <body>

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.

...

""" ``` ## .parent 通过 `.parent` 属性来获取某个元素的父节点 ```python b_tag = soup.b print(b_tag.parent) #打印输出

The Dormouse's story

``` ## .parents 通过元素的 `.parents` 属性可以递归得到元素的所有父辈节点 ```python b_tag = soup.b for parent in b_tag.parents: if parent is None: print(parent) else: print(parent.name) # 打印输出 p body html [document] ``` ## .next_sibling 和 .previous_sibling 兄弟标签,指的两个便签同级别,比如样例结构中,有好多个p标签,他们都是兄弟便签。 ``` sibling_soup = BeautifulSoup("text1text2

标签:story,偷桃,parent,爬虫,Dormouse,soup,scrapy,html,print
来源: https://blog.51cto.com/u_15241290/2961670

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有