本文共 5041 字,大约阅读时间需要 16 分钟。
一.爬虫
1.介绍
爬虫就是通过编写程序模拟浏览器上网,然后让其去互联网上抓取数据的过程
2.分类
通用爬虫: 通用爬虫是搜索引擎(Baidu、Google、Yahoo等)“抓取系统”的重要组成部分
聚焦爬虫:聚焦爬虫是根据指定的需求抓取网络上指定的数据。
3.反爬虫
门户网站通过相应的策略和技术手段,防止爬虫程序进行网站数据的爬取。
4.反反爬虫
爬虫程序通过相应的策略和技术手段,破解门户网站的反爬虫手段,从而爬取到相应的数据。
5.robots.txt协议
如果自己的门户网站中的指定页面中的数据不想让爬虫程序爬取到的话,那么则可以通过编写一个robots.txt的协议文件来约束爬虫程序的数据爬取。
二.jupyter notebook的使用
1.介绍
Jupyter Notebook是以网页的形式打开,可以在网页页面中直接编写代码和运行代码,代码的运行结果也会直接在代码块下显示。如在编程过程中需要编写说明文档,可在同一个页面中直接编写,便于作及时的说明和解释。
2.组成部分
网页应用
文档
3.特点
编程时具有语法高亮、缩进、tab补全的功能。
可直接通过浏览器运行代码,同时在代码块下方展示运行结果。
对代码编写说明文档或语句时,支持Markdown语法。
4.安装
5.运行
默认端口启动
在终端中输入以下命令:
jupyter notebook
指定端口启动
jupyter notebook --port
启动服务器但不打开浏览器
jupyter notebook --no-browser
6.快捷键的使用
- 向上插入一个cell:a
- 向下插入一个cell:b
- 删除cell:x
- 将code切换成markdown:m
- 将markdown切换成code:y
- 运行cell:shift+enter
- 查看帮助文档:shift+tab
- 自动提示:tab
二.requests模块
requests模块代码编写的流程:
指定url
发起请求
获取响应对象中的数据
持久化存储
例子
爬取搜狗的页面
import requests#1url = '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 requestsurl = 'https://www.sogou.com/web'#封装参数wd = input('enter a word:')param = { 'query':wd}response = requests.get(url=url,params=param)page_text = response.contentfileName = 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 requestsurl = '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).contentwith open('./xiaohua.jpg','wb') as fp: fp.write(img_data)
用urllib
import urlliburl = '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 restring = '''fall in love with youi love you very muchi love shei love her'''re.findall('^i.*',string,re.M)
import re
#匹配全部行string1 = """细思极恐你的队友在看书你的敌人在磨刀你的闺蜜在减肥隔壁老王在练腰"""re.findall('.*',string1,re.S)
爬取糗事百科中所有的图片进行保存
import requestsimport reimport urllibimport osurl = 'https://www.qiushibaike.com/pic/page/%d/?s=5170552'# page = 1headers = { '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('.*? ',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!!!')
转载地址:https://www.cnblogs.com/chenxi67/p/10440398.html 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!