1、安装与开发环境
模块安装:
bs4 解析库安装:pip install bs4
开发环境:
python 3.x + pycharm
ps:文章来源于小编的头条号:“python数据科学频道”,
想在手机端方便看文章的朋友可以去关注我的头条号。
2、实训代码如下:
from bs4 import BeautifulSoup
import urllib.request
# -------------------------- 基础库 -------------------------------------
url = 'http://www.imooc.com/course/list?c=python&page=1'
# 设置头文件,将爬虫模拟成浏览器的行为去爬取信息
heads = {
'Connection': 'keep-alive',
'Accept-Language': 'zh-CN,zh;q=0.9',
'Accept': 'text/html,application/xhtml+xml,application/xml;\
q=0.9,image/webp,image/apng,*/*;q=0.8',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36\
(KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36',
}
headall = []
for key,value in heads.items():
items = (key,value)
headall.append(items)
# 创建opener对象
opener = urllib.request.build_opener()
# 添加头文件到opener对象
opener.addheaders = headall
# 将opener对象设置成全局模式
urllib.request.install_opener(opener)
# 将数据读取到data
data = opener.open(url).read()
# print(data)
# --------------------------- 解析提取库 --------------------------------
# 创建soup对象
soup = BeautifulSoup(data,'lxml')
# print(soup)
# 匹配课程名称
coursenames = soup.find_all('h3',class_="course-card-name")
# print(coursenames)
# 匹配课程链接
links = soup.find_all('a',class_="course-card")
# print(links)
name, lk, x= [], [], 1
# 将名称添加至name列表中
for coursename in coursenames:
coursenames = coursename.get_text()
name.append(coursenames)
x += 1 # 主要是为了确认有多少门课
# print(name)
# 将链接添加至lk列表中
for link in links:
links = 'https://www.imooc.com'+str(link['href'])
lk.append(links)
# print(lk)
# 用循环遍历出课程名称以及链接
for i in range(x-1):
print('第',(i+1),'门课程:','名称:',name[i],' ',lk[i])
3、程序运行结果如下:
第 1 门课程: 名称: 走进Python量化交易--入门篇 https://www.imooc.com/learn/1054
第 2 门课程: 名称: Python 异步任务队列 Celery 使用 https://www.imooc.com/learn/1051
第 3 门课程: 名称: python运维-Socket网络编程 https://www.imooc.com/learn/1031
第 4 门课程: 名称: Python最火爬虫框架Scrapy入门与实践 https://www.imooc.com/learn/1017
第 5 门课程: 名称: TensorFlow与Flask结合打造手写体数字识别 https://www.imooc.com/learn/994
第 6 门课程: 名称: 区块链技术核心概念与原理讲解 https://www.imooc.com/learn/988
第 7 门课程: 名称: Python实现线性回归 https://www.imooc.com/learn/972
第 8 门课程: 名称: Python人工智能常用库Numpy使用入门 https://www.imooc.com/learn/943
第 9 门课程: 名称: Python制作数据分析工具 https://www.imooc.com/learn/937
第 10 门课程: 名称: python必学模块-collections https://www.imooc.com/learn/934
第 11 门课程: 名称: Python 全栈案例初体验 https://www.imooc.com/learn/864
第 12 门课程: 名称: python自动化运维篇 https://www.imooc.com/learn/853
第 13 门课程: 名称: Python数据分析-基础技术篇 https://www.imooc.com/learn/843
第 14 门课程: 名称: 机器学习-实现简单神经网络 https://www.imooc.com/learn/813
第 15 门课程: 名称: django入门与实践 https://www.imooc.com/learn/790
第 16 门课程: 名称: python-web.py开发入门 https://www.imooc.com/learn/753
第 17 门课程: 名称: Python-面向对象 https://www.imooc.com/learn/747
第 18 门课程: 名称: Python-走进Requests库 https://www.imooc.com/learn/736
第 19 门课程: 名称: 初识机器学习-理论篇 https://www.imooc.com/learn/717
第 20 门课程: 名称: python遇见数据采集 https://www.imooc.com/learn/712
第 21 门课程: 名称: python装饰器 https://www.imooc.com/learn/581
第 22 门课程: 名称: Python开发简单爬虫 https://www.imooc.com/learn/563
第 23 门课程: 名称: python正则表达式 https://www.imooc.com/learn/550
第 24 门课程: 名称: PyConChina 2015 中国大会 北京场 https://www.imooc.com/learn/532
第 25 门课程: 名称: Python错误和异常 https://www.imooc.com/learn/457
第 26 门课程: 名称: Python操作MySQL数据库 https://www.imooc.com/learn/475
第 27 门课程: 名称: django初体验 https://www.imooc.com/learn/458
第 28 门课程: 名称: Python文件处理 https://www.imooc.com/learn/416
第 29 门课程: 名称: Python开发环境搭建 https://www.imooc.com/learn/397
第 30 门课程: 名称: python进阶 https://www.imooc.com/learn/317
4、程序代码分析如下:
1、关键在于理解bs4的解析提取的方法
"课程名称"的网址源代码:
<h3 class="course-card-name">走进Python量化交易--入门篇</h3>
"课程链接"的原网址代码:
<a target="_blank" href="/learn/1054" class="course-card">
2、bs4介绍
创建soup对象
soup = BeautifulSoup(data,‘lxml’)
这里的属性 class 要加上一个下划线,因为 class 是python内置的关键字,不能直接使用
匹配课程名称
coursenames = soup.find_all(‘h3’,class_=“course-card-name”)
匹配课程链接
links = soup.find_all(‘a’,class_=“course-card”)
-------------------------------------------- 文章到此结束------------------------------------------