Beautifulsoup作用:
将网页(非结构化内容)转化成结构化内容
.text取得bs对象的文字内容(去除HTML标签)
现在令一个新的字符串:
html sample ='
Hello World!
将字符串转化为bs对象:
soup = BeautifulSoup(html_sample)
![](https://i-blog.csdnimg.cn/blog_migrate/276efc8186b6013fea8b45aa3fec9361.webp?x-image-process=image/format,png)
去除警告信息的方法:
原因:因为没有指定“剖析器”
解决:
soup = BeautifulSoup(html_sample,'html.parser')
![](https://i-blog.csdnimg.cn/blog_migrate/b2bf0de4004d5114a88a4962c2388c3b.webp?x-image-process=image/format,png)
找寻特定元素:
#使用select找出含有h1标签的元素
alink = soup.select('h1')
print(alink)
![](https://i-blog.csdnimg.cn/blog_migrate/92fd638a1162eb66d57c41e8c39b86a9.webp?x-image-process=image/format,png)
Print(alink[0])
输出的是内容,没有中括号
![](https://i-blog.csdnimg.cn/blog_migrate/9665223ad432341b0d664d6c4cac3464.webp?x-image-process=image/format,png)
print(alink[0].text)
输出标签内的文字
![](https://i-blog.csdnimg.cn/blog_migrate/a3b555be15f95c9e8b8c3d96a05be324.webp?x-image-process=image/format,png)
#使用select找出含有a标签的元素
alink = soup.select('a')
print(alink)
![](https://i-blog.csdnimg.cn/blog_migrate/7461c404053a4543c0c9be7b32936cd9.webp?x-image-process=image/format,png)
#使用select找出id="title"的内容(id前面需要加上#)感觉就类似CSS语法
alink = soup.select('#title')
print(alink)
![](https://i-blog.csdnimg.cn/blog_migrate/b9cdbb8b21234aadab87efc8e1adefee.webp?x-image-process=image/format,png)
#使用select找出class="link"的内容(class前面需要加上.)
alink = soup.select('.link')
print(alink)
![](https://i-blog.csdnimg.cn/blog_migrate/87e39e341747558f0aed63b3069317ed.webp?x-image-process=image/format,png)
我们看到输出多个结果,以列表的形式存储
相信这样大家就会更加清晰明白了:
![](https://i-blog.csdnimg.cn/blog_migrate/5718fca3f983d381922dfad5fb08c620.webp?x-image-process=image/format,png)
循环输出:
for link in alink:
print(link)
![](https://i-blog.csdnimg.cn/blog_migrate/75e3eb9168dd1fefd2e88a4917cbd2e1.webp?x-image-process=image/format,png)
.text的利用
![](https://i-blog.csdnimg.cn/blog_migrate/178521ff7874fe9f90feb289e2abd8b5.webp?x-image-process=image/format,png)
获取a标签的href属性:
for link in alink:
print(link['href'])
![](https://i-blog.csdnimg.cn/blog_migrate/acba1cdb5853d900de0f2eb6f485456f.webp?x-image-process=image/format,png)
会把href等属性包装成一个字典故很方便地取到数据!
取属性值:
html_sample2 = ' hello world! hello world2!'
soup2 = BeautifulSoup(html_sample2,'html.parser')
print(soup2.select('a')[0]['id'])
print(soup2.select('a')[1]['id'])
![](https://i-blog.csdnimg.cn/blog_migrate/8ae5a48873444c0450377e6438ca023a.webp?x-image-process=image/format,png)
注:本文属于原创文章,转载请注明版权!
CSDN博客主页:http://blog.csdn.net/dyboy2017
Github开源项目:https://github.com/dyboy2017/spider
Beautifulsoup作用:
将网页(非结构化内容)转化成结构化内容
.text取得bs对象的文字内容(去除HTML标签)
现在令一个新的字符串:
html sample ='
Hello World!
将字符串转化为bs对象:
soup = BeautifulSoup(html_sample)
![](https://i-blog.csdnimg.cn/blog_migrate/276efc8186b6013fea8b45aa3fec9361.webp?x-image-process=image/format,png)
去除警告信息的方法:
原因:因为没有指定“剖析器”
解决:
soup = BeautifulSoup(html_sample,'html.parser')
![](https://i-blog.csdnimg.cn/blog_migrate/b2bf0de4004d5114a88a4962c2388c3b.webp?x-image-process=image/format,png)
找寻特定元素:
#使用select找出含有h1标签的元素
alink = soup.select('h1')
print(alink)
![](https://i-blog.csdnimg.cn/blog_migrate/92fd638a1162eb66d57c41e8c39b86a9.webp?x-image-process=image/format,png)
Print(alink[0])
输出的是内容,没有中括号
![](https://i-blog.csdnimg.cn/blog_migrate/9665223ad432341b0d664d6c4cac3464.webp?x-image-process=image/format,png)
print(alink[0].text)
输出标签内的文字
![](https://i-blog.csdnimg.cn/blog_migrate/a3b555be15f95c9e8b8c3d96a05be324.webp?x-image-process=image/format,png)
#使用select找出含有a标签的元素
alink = soup.select('a')
print(alink)
![](https://i-blog.csdnimg.cn/blog_migrate/7461c404053a4543c0c9be7b32936cd9.webp?x-image-process=image/format,png)
#使用select找出id="title"的内容(id前面需要加上#)感觉就类似CSS语法
alink = soup.select('#title')
print(alink)
![](https://i-blog.csdnimg.cn/blog_migrate/b9cdbb8b21234aadab87efc8e1adefee.webp?x-image-process=image/format,png)
#使用select找出class="link"的内容(class前面需要加上.)
alink = soup.select('.link')
print(alink)
![](https://i-blog.csdnimg.cn/blog_migrate/87e39e341747558f0aed63b3069317ed.webp?x-image-process=image/format,png)
我们看到输出多个结果,以列表的形式存储
相信这样大家就会更加清晰明白了:
![](https://i-blog.csdnimg.cn/blog_migrate/5718fca3f983d381922dfad5fb08c620.webp?x-image-process=image/format,png)
循环输出:
for link in alink:
print(link)
![](https://i-blog.csdnimg.cn/blog_migrate/75e3eb9168dd1fefd2e88a4917cbd2e1.webp?x-image-process=image/format,png)
.text的利用
![](https://i-blog.csdnimg.cn/blog_migrate/178521ff7874fe9f90feb289e2abd8b5.webp?x-image-process=image/format,png)
获取a标签的href属性:
for link in alink:
print(link['href'])
![](https://i-blog.csdnimg.cn/blog_migrate/acba1cdb5853d900de0f2eb6f485456f.webp?x-image-process=image/format,png)
会把href等属性包装成一个字典故很方便地取到数据!
取属性值:
html_sample2 = ' hello world! hello world2!'
soup2 = BeautifulSoup(html_sample2,'html.parser')
print(soup2.select('a')[0]['id'])
print(soup2.select('a')[1]['id'])
![](https://i-blog.csdnimg.cn/blog_migrate/8ae5a48873444c0450377e6438ca023a.webp?x-image-process=image/format,png)
注:本文属于原创文章,转载请注明版权!
CSDN博客主页:http://blog.csdn.net/dyboy2017
Github开源项目:https://github.com/dyboy2017/spider