Beautiful Soup提供一些简单的、python式的函数用来处理导航、搜索、修改分析树等功能。它是一个
工具箱,通过解析文档为tiful Soup自动将输入文档转换为Unicode编码,输出文档转换为utf-8编码。
你不需要考虑编码方式,除非文档没有指定一个编一下原始编码方式就可以了。
Tag就是html中的一个标签,用BeautifulSoup就能解析出来Tag的具体内容,
具体的格式为soup.name,其中name是html下的标签。
from bs4 import BeautifulSoup
html = """
story12345
The Dormouse's story
Once upon a time there were three little sisters; and their names were
westos,
Lacie and
Tillie;
and they lived at the bottom of a well.
...
"""
soup = BeautifulSoup(html, 'html.parser')
print(soup.prettify()) #使格式输出
# 1. 根据标签获取内容;
# ******************标签的常用属性************************
# 根据格式化, 如果title只有一个, 根据标签可以获取
print(soup.title)
print(type(soup.title))
print(soup.title.name) # 标签的名称
# 获取标签里面的属性信息
print(soup.a.attrs)
print(soup.a.attrs['href'])
# # *******************标签常用的方法*************************
#get方法用于得到标签下的属性值,注意这是一个重要的方法,在许多场合都能用到,比如你要得到
标签下的图像url,那么就可以用soup.img.get(‘src’)
print(soup.a.get('href'))
print(soup.a.get('class'))
# string得到标签下的文本内容,只有在此标签下没有子标签,或者只有一个子标签的情况下才能返回其中的内容,否则返回的是None;
# get_text()可以获得一个标签中的所有文本内容,包括子孙节点的内容,这是最常用的方法
print(soup.a.string) # 标签里面的内容
print(soup.a.get_text())
# # *******************对获取的属性信息进行修改***********************
print(soup.a.get('href'))
soup.a['href'] = 'http://www.baidu.com'
print(soup.a.get('href'))
print(soup.a)
# # 查找符合条件的所有标签;
aTagObj = soup.find_all('a')
print(aTagObj)
for item in aTagObj:
print(item)
# 需求: 获取所有的a标签, 并且类名为"sister"
aTagObj = soup.find_all('a', class_="sister")
print(aTagObj)
3.根据内容进行匹配
import re
print(soup.find_all(text="story"))
print(soup.find_all(text=re.compile('story\d+')))
soup = BeautifulSoup(html, 'html.parser')
# 需要安装第三方模块lxml;
# soup = BeautifulSoup(html, 'lxml')
# 1. 返回符合条件的第一个标签内容
print(soup.title)
print(soup.p)
print(soup.find('p', class_=re.compile(r'^ti.*?'))) #.*+?--->非贪婪模式
# 2. 返回符合条件的所有标签内容
print(soup.find_all('p'))
print(soup.find_all('p', class_='title', text=re.compile(r'.*?story.*?')))
# 3. 获取符合条件的p标签或者a标签
print(soup.find(['title', 'a']))
print(soup.find_all(['title', 'a']))
print(soup.find_all(['title', 'a'], class_=['title', 'sister']))
# 4. CSS匹配
# 标签选择器
print(soup.select("title"))
# 类选择器(.类名)
print(soup.select(".sister"))
# id选择器(#id名称)
print(soup.select("#link1"))
# 此处不支持正则表达式;
# print(soup.select(re.compile("#link\d+")))
# 属性选择器()
print(soup.select("input[type='password']"))
官方中文文档: https://beautifulsoup.readthedocs.io/zh_CN/v4.4.0/
下表列出了主要的解析器,以及它们的优缺点:
解析器:使用方法:优势:劣势
import requests
from bs4 import BeautifulSoup
def get_content(url,):
try:
user_agent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.109 Safari/537.36"
response = requests.get(url, headers={'User-Agent': user_agent})
response.raise_for_status() # 如果返回的状态码不是200, 则抛出异常;
response.encoding = response.apparent_encoding # 判断网页的编码格式, 便于respons.text知道如何解码;
except Exception as e:
print("爬取错误")
else:
print(response.url)
print("爬取成功!")
return response.content
def parser_content(htmlContent):
# 实例化soup对象, 便于处理;
soup = BeautifulSoup(htmlContent, 'html.parser')
# 提取页面的头部信息, 解决乱码问题
headObj = soup.head
# 提取需要的内容;
divObj = soup.find_all('div', class_="blog-content-box")[0]
#
scriptObj = soup.script
with open('doc/csdn.html', 'w') as f:
# 写入头部信息(指定编码格式和标题)
f.write(str(headObj))
# 写入博客正文;
f.write(str(divObj))
print("下载成功......")
# f.write(str(scriptObj))
if __name__ == '__main__':
url = "https://blog.csdn.net/mkgdjing/article/details/87776319"
content = get_content(url)
parser_content(content)
import requests
from bs4 import BeautifulSoup
import re
def get_content(url,):
try:
user_agent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.109 Safari/537.36"
response = requests.get(url, headers={'User-Agent': user_agent})
response.raise_for_status() # 如果返回的状态码不是200, 则抛出异常;
response.encoding = response.apparent_encoding # 判断网页的编码格式, 便于respons.text知道如何解码;
except Exception as e:
print("爬取错误")
else:
print(response.url)
print("爬取成功!")
return response.content
def parser_content(htmlContent):
# 实例化soup对象, 便于处理;
soup = BeautifulSoup(htmlContent, 'html.parser')
# 1). 获取每个博客的大盒子: 特征: div标签, class名称一致article-item-box csdn-tracking-statistics
#
divObjs = soup.find_all('div', class_="article-item-box")
# 2). 依次遍历每一个div标签, 获取博客标题
# 博客标题的特征: h4里面的a标签里面的内容
# 去掉默认的广告, 留下个人的博客内容;
for divObj in divObjs[1:]:
# **2-1. 获取博客标题: 去掉原创或者转载的信息, 只需要博客名称;
title = divObj.h4.a.get_text().split()[1]
# **2-2. 获取博客链接, 也就是获取a链接中href对应的值;
blogUrl = divObj.h4.a.get('href')
global bloginfo
# 将爬取的所有内容保存到变量中[(blogtitle, blogurl)]
bloginfo.append((title, blogUrl))
if __name__ == '__main__':
blogPage = 3
# 全局变量, 用于保存所有博客信息;
bloginfo = []
for page in range(1, blogPage+1):
url = "https://blog.csdn.net/mkgdjing/article/list/%s" %(page)
content = get_content(url)
parser_content(content)
print("第%d页整理结束...." %(page))
with open('doc/myblog.md', 'a') as f:
for index, info in enumerate(bloginfo[::-1]):
f.write('- 第%d篇博客: [%s](%s)\n' %(index+1, info[0], info[1]))
print("完成.....")