欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Python BeautifulSoup 使用

程序员文章站 2022-04-10 22:13:45
BS4库简单使用: 1.最好配合LXML库,下载:pip install lxml 2.最好配合Requests库,下载:pip install requests 3.下载bs4:pip install bs4 4.直接输入pip没用?解决:环境变量->系统变量->Path->新建:C:\Pytho ......
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'])