1. 如何查看网页结构
以豆瓣网为例:https://book.douban.com/subject_search?search_text=%E6%95%B0%E6%8D%AE&cat=1001
1)浏览器:谷歌浏览器 - chrome
2)开启开发者模式:右键 → 检查
3)查看源代码:右键 → 查看网页源代码
2. 如何构建爬虫逻辑
2.1 一个简单的框架图
2.2 爬虫基本逻辑(一):【分页网页url采集】-【数据信息网页url采集】-【数据采集】
该逻辑1个数据信息网页采集1条数据
第一步【分页网页url采集】→ 得到一个分页的urllst1
① 找到分页网址 → 比如这里以之前的爬取实习僧网站的python实习生的信息举例
https://www.shixiseng.com/interns?page=1&keyword=python&type=intern&area=&months=&days=°ree=&official=&enterprise=&salary=-0&publishTime=&sortType=&city=%E5%8C%97%E4%BA%AC&internExtend=
https://www.shixiseng.com/interns?page=2&keyword=python&type=intern&area=&months=&days=°ree=&official=&enterprise=&salary=-0&publishTime=&sortType=&city=%E5%8C%97%E4%BA%AC&internExtend=
https://www.shixiseng.com/interns?page=3&keyword=python&type=intern&area=&months=&days=°ree=&official=&enterprise=&salary=-0&publishTime=&sortType=&city=%E5%8C%97%E4%BA%AC&internExtend=
。。。。。。
② 这里由于网页只需要更改“page=…”,所以通过for循环即可
第二步【数据信息网页url采集】→ 得到一个数据页的urllst2
① 基于分页网址urllst1,采集每一个数据页面的url,并存入urllst2
https://www.shixiseng.com/intern/inn_j34ozcntlsab
https://www.shixiseng.com/intern/inn_fxckjairtwke
https://www.shixiseng.com/intern/inn_2bjwgimxguda
② 这里需要用到requests + BeautifulSoup实现
第三步【数据采集】→ 每条数据存进一个dict,所有dict组成一个datalst列表 ① 通过BeautisulSoup解析标签,采集数据
② 通过BeautisulSoup实现(接下来要将的内容)
2.3 爬虫基本逻辑(二):【分页网页url采集】-【数据采集】
该逻辑也可以称为:“循环标签采集”:1个分页网页采集n条数据
优势:相比于第一种逻辑,访问网页次数较少,容易避开反爬
劣势:相比于第一种逻辑,获取信息较少
第一步【分页网页url采集】→ 得到一个分页的urllst1
① 找到分页网址 → 比如这里以之前的爬取实习僧网站的python实习生的信息举例
https://www.shixiseng.com/interns?page=1&keyword=python&type=intern&area=&months=&days=°ree=&official=&enterprise=&salary=-0&publishTime=&sortType=&city=%E5%8C%97%E4%BA%AC&internExtend=
https://www.shixiseng.com/interns?page=2&keyword=python&type=intern&area=&months=&days=°ree=&official=&enterprise=&salary=-0&publishTime=&sortType=&city=%E5%8C%97%E4%BA%AC&internExtend=
https://www.shixiseng.com/interns?page=3&keyword=python&type=intern&area=&months=&days=°ree=&official=&enterprise=&salary=-0&publishTime=&sortType=&city=%E5%8C%97%E4%BA%AC&internExtend=
② 这里由于网页只需要更改“page=…”,所以通过for循环即可
第二步【数据采集】→ 每条数据存进一个dict,所有dict组成一个datalst列表 ① 通过for循环依次采集该页面的多个标签
② 通过BeautisulSoup实现,进行网页的解析,获取标签信息全部存到dict里面
3.需要掌握的内容
网络资源访问工具:requests
掌握requests工具包,学会通过python访问网站,并做简单的内容识别
网页信息解析方法:Xpath与BeautifulSoup
掌握BeautifulSoup工具包,理解xpath网页解析方法,基本掌握静态网页的页面数据识别
爬虫数据库:MongoDB
掌握非关系数据库MongoDB,并且学会用python连接及使用MongoDB,管理采集数据