一.爬虫html
1.介绍web
爬虫就是经过编写程序模拟浏览器上网,而后让其去互联网上抓取数据的过程正则表达式
2.分类编程
通用爬虫: 通用爬虫是搜索引擎(Baidu、Google、Yahoo等)“抓取系统”的重要组成部分json
聚焦爬虫:聚焦爬虫是根据指定的需求抓取网络上指定的数据。浏览器
3.反爬虫服务器
门户网站经过相应的策略和技术手段,防止爬虫程序进行网站数据的爬取。markdown
4.反反爬虫网络
爬虫程序经过相应的策略和技术手段,破解门户网站的反爬虫手段,从而爬取到相应的数据。app
5.robots.txt协议
若是本身的门户网站中的指定页面中的数据不想让爬虫程序爬取到的话,那么则能够经过编写一个robots.txt的协议文件来约束爬虫程序的数据爬取。
二.jupyter notebook的使用
1.介绍
Jupyter Notebook是以网页的形式打开,能够在网页页面中直接编写代码和运行代码,代码的运行结果也会直接在代码块下显示。如在编程过程当中须要编写说明文档,可在同一个页面中直接编写,便于做及时的说明和解释。
2.组成部分
网页应用
文档
3.特色
编程时具备语法高亮、缩进、tab补全的功能。
可直接经过浏览器运行代码,同时在代码块下方展现运行结果。
对代码编写说明文档或语句时,支持Markdown语法。
4.安装
5.运行
默认端口启动
在终端中输入如下命令:
jupyter notebook
指定端口启动
jupyter notebook --port <port_number>
启动服务器但不打开浏览器
jupyter notebook --no-browser
6.快捷键的使用
二.requests模块
requests模块代码编写的流程:
指定url
发起请求
获取响应对象中的数据
持久化存储
例子
爬取搜狗的页面
import requests #1 url = 'https://www.sogou.com/' #2. response = requests.get(url=url) #3. page_text = response.text #4. with open('./sogou.html','w',encoding='utf-8') as fp: fp.write(page_text)
#需求:爬取搜狗指定词条搜索后的页面数据 import requests url = 'https://www.sogou.com/web' #封装参数 wd = input('enter a word:') param = { 'query':wd } response = requests.get(url=url,params=param) page_text = response.content fileName = wd+'.html' with open(fileName,'wb') as fp: fp.write(page_text) print('over')
#爬取百度翻译结果 url = 'https://fanyi.baidu.com/sug' wd = input('enter a word:') data = { 'kw':wd } response = requests.post(url=url,data=data) print(response.json()) #response.text : 字符串 #.content : 二进制 #.json() : 对象
#爬取豆瓣电影分类排行榜 https://movie.douban.com/中的电影详情数据 url = 'https://movie.douban.com/j/chart/top_list' param = { "type": "5", "interval_id": "100:90", "action": '', "start": "60", "limit": "100", } movie_data = requests.get(url=url,params=param).json() print(movie_data)
User-Agent参数,简称为UA,该参数的做用是用于代表本次请求载体的身份标识。
#需求:爬取国家药品监督管理总局中基于中华人民共和国化妆品生产许可证相关数据http://125.35.6.84:81/xk/ #反爬机制:UA检测 --> UA假装 import requests url = 'http://125.35.6.84:81/xk/itownet/portalAction.do?method=getXkzsList' headers = { 'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36' } id_list = [] for page in range(1,11): data = { "on": "true", "page": str(page), "pageSize": "15", "productName": "", "conditionType": "1", "applyname": "", "applysn": "", } json_data = requests.post(url=url,data=data,headers=headers).json() for dic in json_data['list']: id = dic['ID'] id_list.append(id) detail_url = 'http://125.35.6.84:81/xk/itownet/portalAction.do?method=getXkzsById' for id in id_list: detail_data = { 'id':id } detail_json = requests.post(url=detail_url,data=detail_data,headers=headers).json() print(detail_json)
urllib是Python自带的一个用于爬虫的库,其主要做用就是能够经过代码模拟浏览器发送请求。其常被用到的子模块在Python3中的为urllib.request和urllib.parse,在Python2中是urllib和urllib2。
#爬取照片 url = 'https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=806201715,3137077445&fm=26&gp=0.jpg' img_data = requests.get(url=url,headers=headers).content with open('./xiaohua.jpg','wb') as fp: fp.write(img_data)
用urllib
import urllib url = 'https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=806201715,3137077445&fm=26&gp=0.jpg' urllib.request.urlretrieve(url=url,filename='./123.jpg')
三.正则表达式
单字符: . : 除换行之外全部字符 [] :[aoe] [a-w] 匹配集合中任意一个字符 \d :数字 [0-9] \D : 非数字 \w :数字、字母、下划线、中文 \W : 非\w \s :全部的空白字符包,括空格、制表符、换页符等等。等价于 [ \f\n\r\t\v]。 \S : 非空白 数量修饰: * : 任意屡次 >=0 + : 至少1次 >=1 ? : 无关紧要 0次或者1次 {m} :固定m次 hello{3,} {m,} :至少m次 {m,n} :m-n次 边界: $ : 以某某结尾 ^ : 以某某开头 分组: (ab) 贪婪模式: .* 非贪婪(惰性)模式: .*? re.I : 忽略大小写 re.M :多行匹配 re.S :单行匹配 re.sub(正则表达式, 替换内容, 字符串)
import re string = '''fall in love with you i love you very much i love she i love her''' re.findall('^i.*',string,re.M)
import re
#匹配所有行 string1 = """细思极恐 你的队友在看书 你的敌人在磨刀 你的闺蜜在减肥 隔壁老王在练腰 """ re.findall('.*',string1,re.S)
爬取糗事百科中全部的图片进行保存
import requests import re import urllib import os url = 'https://www.qiushibaike.com/pic/page/%d/?s=5170552' # page = 1 headers = { 'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36' } if not os.path.exists('./qiutu'): os.mkdir('./qiutu') start_page = int(input('enter a start pageNum:')) end_page = int(input('enter a end pageNum:')) for page in range(start_page,end_page+1): new_url = format(url%page) # print(new_url) page_text = requests.get(url=new_url,headers=headers).text img_url_list = re.findall('<div class="thumb">.*?<img src="(.*?)" alt=.*?</div>',page_text,re.S) for img_url in img_url_list: img_url = 'https:'+img_url imgName = img_url.split('/')[-1] imgPath = 'qiutu/'+imgName urllib.request.urlretrieve(url=img_url,filename=imgPath) print(imgPath,'下载成功!') print('over!!!')