Python—爬虫之BeautifulSoup模块(解析—提取数据)
2019-06-26 16:37
996 查看
Python—爬虫之BeautifulSoup模块(解析—提取数据)
安装BeautifulSoup模块
Mac电脑:打开终端软件(terminal),输入pip3 install BeautifulSoup4,点击enter;
Windows电脑:打开命令提示符(cmd),输入pip install BeautifulSoup4,点击enter;
解析数据
#案例说明 import requests #导入requests库 from bs4 import BeautifulSoup #引入BS库 #利用requests.get()获取网页数据 res = requests.get('https://localprod.pandateacher.com/python-manuscript/crawler-html/spider-men5.0.html') html = res.text #解析为文本数据 soup = BeautifulSoup(html,'html.parser') #把网页解析为BeautifulSoup对象 print(type(soup)) #查看类型。结果是一个<class 'bs4.BeautifulSoup'>对象,便于后面提取数据 print(" ") print(soup)
你敲一遍代码,可以看到结果是HTML结构的网页语言,这就是我们用BeautifulSoup解析网页的结果;
提取数据
find:提取出满足条件的第一个数据
import requests from bs4 import BeautifulSoup url = 'https://localprod.pandateacher.com/python-manuscript/crawler-html/spder-men0.0.html' res = requests.get (url) print(res.status_code) #获取响应码,查看是否成功响应,结果为200则为成功响应 soup = BeautifulSoup(res.text,'html.parser') item = soup.find('div') #使用find()方法提取首个<div>元素,并放到变量item里。 print(type(item)) #打印item的数据类型,类型为:<class 'bs4.element.Tag'> print(item)
find_all:提取出满足条件所有数据
import requests # 调用requests库 from bs4 import BeautifulSoup # 调用BeautifulSoup库 # 返回一个Response对象,赋值给res res = requests.get('https://localprod.pandateacher.com/python-manuscript/crawler-html/spider-men5.0.html') html= res.text # 把Response对象的内容以字符串的形式返回 soup = BeautifulSoup( html,'html.parser') # 把网页解析为BeautifulSoup对象 items = soup.find_all(class_='books') # 通过定位标签和属性提取我们想要的数据 for item in items: #通过循环把数据取出来 print('想找的数据都包含在这里了:\n',item) # 打印item,结果仍然是包含html标签的数据
Tag:对象的属性和方法
import requests # 调用requests库 from bs4 import BeautifulSoup # 调用BeautifulSoup库 res =requests.get('https://localprod.pandateacher.com/python-manuscript/crawler-html/spider-men5.0.html') html=res.text soup = BeautifulSoup( html,'html.parser') # 把网页解析为BeautifulSoup对象 items = soup.find_all(class_='books') # 通过匹配属性class='books'提取出我们想要的元素 for item in items: # 遍历列表items kind = item.find('h2') # 在列表中的每个元素里,匹配标签<h2>提取出数据 title = item.find(class_='title') # 在列表中的每个元素里,匹配属性class_='title'提取出数据 brief = item.find(class_='info') # 在列表中的每个元素里,匹配属性class_='info'提取出数据 print(kind.text,'\n',title.text,'\n',title['href'],'\n',brief.text) # 打印书籍的类型、名字、链接和简介的文字
相关文章推荐
- python爬虫数据提取-BeautifulSoup库
- Python3,通过re模块中的sub()和findall()2个方法提升爬虫提取数据的效率
- Python爬虫---提取数据(2)--beautifulsoup
- Python 爬虫(以赛马数据为例)之使用BeautifulSoup进行Html解析
- python3爬虫学习之数据提取之beautifulsoup
- 初识python爬虫 Python网络数据采集1.0 BeautifulSoup通过网站css爬取信息
- Python基础三「json数据解析,文件处理,多模块」
- 【Python爬虫2】网页数据提取
- python爬虫主要就是五个模块:爬虫启动入口模块,URL管理器存放已经爬虫的URL和待爬虫URL列表,html下载器,html解析器,html输出器 同时可以掌握到urllib2的使用、bs4(BeautifulSoup)页面解析器、re正则表达式、urlparse、python基础知识回顾(set集合操作)等相关内容。
- python的【爬虫】:使用urllib爬取wiki文章,使用beautifulSoup解析html
- python 跨语言数据交互、json、pickle(序列化)、urllib、requests(爬虫模块)、XML。
- 【Python爬虫系列】内容解析之BeautifulSoup
- 开始学习python爬虫,xml.dom模块解析xml
- Python json 数据提取 jsonpath 模块
- Python爬虫:用BeautifulSoup进行NBA数据爬取
- python3爬虫学习之数据提取之lxml与xpath
- Python网络爬虫之数据解析(二)
- python-爬虫-Beautifulsoup模块
- 基于百度API接口的python数据爬虫解析1【笔记】