博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python BeautifulSoup 使用
阅读量:5061 次
发布时间:2019-06-12

本文共 3545 字,大约阅读时间需要 11 分钟。

BS4库简单使用:
1.最好配合LXML库,下载:pip install lxml
2.最好配合Requests库,下载:pip install requests
3.下载bs4:pip install bs4
4.直接输入pip没用?解决:环境变量->系统变量->Path->新建:C:\Python27\Scripts
 
案例:获取网站标题
# -*- coding:utf-8 -*-
from bs4 import BeautifulSoup
import requests
 
url = "https://www.baidu.com"
 
response = requests.get(url)
 
soup = BeautifulSoup(response.content, 'lxml')
 
print soup.title.text
 
标签识别
示例1:
# -*- coding:utf-8 -*-
from bs4 import BeautifulSoup
 
html = '''
<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/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>
<p class="story">...</p>
</body>
</html>
'''
soup = BeautifulSoup(html, 'lxml')
 
# BeautifulSoup中有内置的方法来实现格式化输出
print(soup.prettify())
 
# title标签内容
print(soup.title.string)
 
# title标签的父节点名
print(soup.title.parent.name)
 
# 标签名为p的内容
print(soup.p)
 
# 标签名为p的class内容
print(soup.p["class"])
 
# 标签名为a的内容
print(soup.a)
 
# 查找所有的字符a
print(soup.find_all('a'))
 
# 查找id='link3'的内容
print(soup.find(id='link3'))
 
示例2:
# -*- coding:utf-8 -*-
from bs4 import BeautifulSoup
 
html = '''
<html>
<head><title>The Dormouse's story</title></head>
<body>
<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>
<p class="story">...</p>
</body>
</html>
'''
 
soup = BeautifulSoup(html, 'lxml')
 
# 将p标签下的所有子标签存入到了一个列表中
print (soup.p.contents)
 
find_all示例:
# -*- coding:utf-8 -*-
from bs4 import BeautifulSoup
 
html = '''
<div class="panel">
    <div class="panel-heading">
        <h4>Hello</h4>
    </div>
    <div class="panel-body">
        <ul class="list" id="list-1">
            <li class="element">Foo</li>
            <li class="element">Bar</li>
            <li class="element">Jay</li>
        </ul>
        <ul class="list list-small" id="list-2">
            <li class="element">Foo</li>
            <li class="element">Bar</li>
        </ul>
    </div>
</div>
'''
 
soup = BeautifulSoup(html, 'lxml')
 
# 查找所有的ul标签内容
print(soup.find_all('ul'))
 
# 针对结果再次find_all,从而获取所有的li标签信息
for ul in soup.find_all('ul'):
    print(ul.find_all('li'))
 
# 查找id为list-1的内容
print(soup.find_all(attrs={'id': 'list-1'}))
 
# 查找class为element的内容
print(soup.find_all(attrs={'class': 'element'}))
 
# 查找所有的text='Foo'的文本
print(soup.find_all(text='Foo'))
 
CSS选择器示例:
# -*- coding:utf-8 -*-
from bs4 import BeautifulSoup
 
html = '''
<div class="panel">
    <div class="panel-heading">
        <h4>Hello</h4>
    </div>
    <div class="panel-body">
        <ul class="list" id="list-1">
            <li class="element">Foo</li>
            <li class="element">Bar</li>
            <li class="element">Jay</li>
        </ul>
        <ul class="list list-small" id="list-2">
            <li class="element">Foo</li>
            <li class="element">Bar</li>
        </ul>
    </div>
</div>
'''
 
soup = BeautifulSoup(html, 'lxml')
 
# 获取class名为panel下panel-heading的内容
print(soup.select('.panel .panel-heading'))
 
# 获取class名为ul和li的内容
print(soup.select('ul li'))
 
# 获取class名为element,id为list-2的内容
print(soup.select('#list-2 .element'))
 
# 使用get_text()获取文本内容
for li in soup.select('li'):
    print(li.get_text())
 
# 获取属性的时候可以通过[属性名]或者attrs[属性名]
for ul in soup.select('ul'):
    print(ul['id'])
    # print(ul.attrs['id'])
 

转载于:https://www.cnblogs.com/xuyiqing/p/10295367.html

你可能感兴趣的文章
在qq中可以使用添加标签功能
查看>>
eclipse 自定义布局
查看>>
团队项目开发客户端——登录子系统的设计
查看>>
【AppScan心得】IBM Rational AppScan 无法记录登录序列
查看>>
[翻译] USING GIT IN XCODE [4] 在XCODE中使用GIT[4]
查看>>
简化通知中心的使用
查看>>
SpringMVC的@Validated校验注解使用方法
查看>>
Python之os模块
查看>>
IO—》Properties类&序列化流与反序列化流
查看>>
【蓝桥杯】PREV-21 回文数字
查看>>
html 简介
查看>>
python使用上下文对代码片段进行计时,非装饰器
查看>>
js中比较实用的函数用法
查看>>
安装预览版镜像后无法检测到预览版更新的解决方案
查看>>
【bzoj5099】[POI2018]Pionek 双指针法
查看>>
别让安全问题拖慢了 DevOps!
查看>>
JAR打包和运行
查看>>
session如何保存在专门的StateServer服务器中
查看>>
react展示数据
查看>>
测试计划
查看>>