爬虫学习参考博客:https://www.cnblogs.com/cyycyhcbw/articles/10442399.html
1.使用软件anaconda
2.输入jupyter notbook
3.anaconda使用快捷键
-插入cell: a 上一行插入 b 下一行插入
-删除: x
-模式切换: m切换到master y 切换到code
-执行cell:shift+enter
-tab
-打开帮助文档:shift+tab
4.爬虫概述
通过编写程序模拟浏览器上网,然后让其去互联网上爬取数据的过程
5.爬虫的分类:
5.1通用爬虫:爬取一整张页面源码数据
5.2聚焦爬虫:爬取页面中指定的局部数据
5.3增量式爬虫:检测网站数据更新的情况,爬取的就是网站中最新的数据
6.反爬机制
7.反反爬策略
8.第一个反爬机制:robots.txt协议
二:requests模块的应用
-requests:功能强大,操作简单
-urllib
-作用:用来模拟浏览器发请求
-编码流程
-指定url
-发起请求:requests.get/post
-获取响应数据
-持久化存储
#爬取搜狗首页的源码数据 import requests #1. url="https://www.sogou.com/" #2. response=requestes.get(url=url) #3. page_text=response.text #4. with open('/sougou.html','w',encoding='utf-8') as fp: fp.write(page_text)
#简易的网页采集器 wd=input('enter a word') #1. url="https://www.sogou.com/web?" #2.将请求参数设定为动态的 param={ "query":wd } #3.params传参 response=requests.get(url=url,params=param) #4.手动设置响应数据的编码,处理中文乱码问题 response.encoding='utf-8' #5.text返回的是字符串形式的响应数据 page_text=response.text filename=wd+'.html' with open(filename,'w',enconding='