1..string 属性是用来获取节点中标签内容。
- for content in content_all:
- contentString = content.string
- print(contentString)
.string 属性只能提取单个节点或节点统一的内容。
提取节点包含多个子节点时:使用这个属性时,不清楚应该调用哪个节点的内容,会返回None值。
2.获取全部的文本内容
.text 属性,是获取节点中的所有文字内容。
- for content in content_all:
-
- contentString=content.text
-
- print(contentString)
3.如何定位节点
(1)右键点击检查
(2)快捷键ctrl+F弹出搜索框
(3)输入我们要查找的,就会显示其出现的第一个位置;
4.实战演练
- import requests
-
- from bs4 import BeautifulSoup
-
- for num in range(1,6):
-
- url=f"https://ssr1.scrape.center/page/{num}"
-
- response=requests.get(url)
-
- html=response.text
-
- soup=BeautifulSoup(html,"lxml")
-
- name_all=soup.find_all(name="h2")
-
- for item in name_all:
-
- name=item.string
-
- print(name)