python爬虫学习笔记-网络爬虫的三种数据解析方式

爬虫的分类php

1.通用爬虫:通用爬虫是搜索引擎(Baidu、Google、Yahoo等)“抓取系统”的重要组成部分。主要目的是将互联网上的网页下载到本地,造成一个互联网内容的镜像备份。  简单来说就是尽量的;把互联网上的全部的网页下载下来,放到本地服务器里造成备分,在对这些网页作相关处理(提取关键字、去掉广告),最后提供一个用户检索接口。 
    搜索引擎如何抓取互联网上的网站数据?
      门户网站主动向搜索引擎公司提供其网站的url
      搜索引擎公司与DNS服务商合做,获取网站的url
      门户网站主动挂靠在一些知名网站的友情连接中
2.聚焦爬虫:聚焦爬虫是根据指定的需求抓取网络上指定的数据。例如:获取豆瓣上电影的名称和影评,而不是获取整张页面中全部的数据值。

robots.txt协议
    - 若是本身的门户网站中的指定页面中的数据不想让爬虫程序爬取到的话,那么则能够经过编写一个robots.txt的协议文件来约束爬虫程序的数据爬取。robots协议的编写格式能够观察淘宝网的robots(访问www.taobao.com/robots.txt便可)。可是须要注意的是,该协议只是至关于口头的协议,并无使用相关技术进行强制管制,因此该协议是防君子不防小人。可是咱们在学习爬虫阶段编写的爬虫程序能够先忽略robots协议。
反爬虫
   - 门户网站经过相应的策略和技术手段,防止爬虫程序进行网站数据的爬取。
反反爬虫
   - 爬虫程序经过相应的策略和技术手段,破解门户网站的反爬虫手段,从而爬取到相应的数据。

  

网络爬虫之requests模块html

  • 基于requests的get请求
  • 基于requests模块的post请求
  • 基于requests模块ajax的get请求
  • 基于requests模块ajax的post请求
  • 综合项目练习:爬取国家药品监督管理总局中基于中华人民共和国化妆品生产许可证相关数据

- 基于以下5点展开requests模块的学习

  • 什么是requests模块
    • requests模块是python中原生的基于网络请求的模块,其主要做用是用来模拟浏览器发起请求。功能强大,用法简洁高效。在爬虫领域中占据着半壁江山的地位。
  • 为何要使用requests模块
    • 由于在使用urllib模块的时候,会有诸多不便之处,总结以下:
      • 手动处理url编码
      • 手动处理post请求参数
      • 处理cookie和代理操做繁琐
      • ......
    • 使用requests模块:
      • 自动处理url编码
      • 自动处理post请求参数
      • 简化cookie和代理操做
      • ......
  • 如何使用requests模块
    • 安装:
      • pip install requests
    • 使用流程
      • 指定url
      • 基于requests模块发起请求
      • 获取响应对象中的数据值
      • 持久化存储

python网络爬虫的三种解析方式java

  • 正则解析
  • xpath解析
  • bs4解析

一.常见的正解解析

单字符:
        . : 除换行之外全部字符
        [] :[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 #提取出python
key="javapythonc++php" re.findall('python',key)[0] ################################################# #提取出hello world
key="<html><h1>hello world<h1></html>" re.findall('<h1>(.*)<h1>',key)[0] ################################################# #提取170
string = '我喜欢身高为170的女孩' re.findall('\d+',string) ################################################# #提取出http://和https://
key='http://www.baidu.com and https://boob.com' re.findall('https?://',key) ################################################# #提取出hello
key='lalala<hTml>hello</HtMl>hahah' #输出<hTml>hello</HtMl>
re.findall('<[Hh][Tt][mM][lL]>(.*)</[Hh][Tt][mM][lL]>',key) ################################################# #提取出hit. 
key='bobo@hit.edu.com'#想要匹配到hit.
re.findall('h.*?\.',key) ################################################# #匹配sas和saas
key='saas and sas and saaas' re.findall('sa{1,2}s',key) ################################################# #匹配出i开头的行
string = '''fall in love with you i love you very much i love she i love her''' re.findall('^.*',string,re.M) ################################################# #匹配所有行
string1 = """<div>静夜思 窗前明月光 疑是地上霜 举头望明月 低头思故乡 </div>""" re.findall('.*',string1,re.S)
正则使用小练习
#!/usr/bin/env python # -*- coding:utf-8 -*-
import requests import re import os if __name__ == "__main__": url = 'https://www.qiushibaike.com/pic/%s/' headers={ 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36', } #指定起始也结束页码
     page_start = int(input('enter start page:')) page_end = int(input('enter end page:')) #建立文件夹
     if not os.path.exists('images'): os.mkdir('images') #循环解析且下载指定页码中的图片数据
     for page in range(page_start,page_end+1): print('正在下载第%d页图片'%page) new_url = format(url % page) response = requests.get(url=new_url,headers=headers) #解析response中的图片连接
         e = '<div class="thumb">.*?<img src="(.*?)".*?>.*?</div>' pa = re.compile(e,re.S) image_urls = pa.findall(response.text) #循环下载该页码下全部的图片数据
         for image_url in image_urls: image_url = 'https:' + image_url image_name = image_url.split('/')[-1] image_path = 'images/'+image_name image_data = requests.get(url=image_url,headers=headers).content with open(image_path,'wb') as fp: fp.write(image_data)
项目需求:爬取糗事百科指定页面的糗图,并将其保存到指定文件夹中

 

二.Xpath解析

经常使用的xpath表达式:

属性定位: #找到class属性值为song的div标签 //div[@class="song"] 层级&索引定位: #找到class属性值为tang的div的直系子标签ul下的第二个子标签li下的直系子标签a //div[@class="tang"]/ul/li[2]/a 逻辑运算: #找到href属性值为空且class属性值为du的a标签 //a[@href="" and @class="du"] 模糊匹配: //div[contains(@class, "ng")] //div[starts-with(@class, "ta")] 取文本: # /表示获取某个标签下的文本内容 # //表示获取某个标签下的文本内容和全部子标签下的文本内容 //div[@class="song"]/p[1]/text() //div[@class="tang"]//text() 取属性: //div[@class="tang"]//li[2]/a/@href
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <title>测试bs4</title>
</head>
<body>
    <div>
        <p>百里守约</p>
    </div>
    <div class="song">
        <p>李清照</p>
        <p>王安石</p>
        <p>苏轼</p>
        <p>柳宗元</p>
        <a href="http://www.song.com/" title="赵匡胤" target="_self">
            <span>this is span</span> 宋朝是最强大的王朝,不是军队的强大,而是经济很强大,国民都颇有钱</a>
        <a href="" class="du">总为浮云能蔽日,长安不见令人愁</a>
        <img src="http://www.baidu.com/meinv.jpg" alt="" />
    </div>
    <div class="tang">
        <ul>
            <li><a href="http://www.baidu.com" title="qing">清明时节雨纷纷,路上行人欲断魂,借问酒家何处有,牧童遥指杏花村</a></li>
            <li><a href="http://www.163.com" title="qin">秦时明月汉时关,万里长征人未还,但使龙城飞将在,不教胡马度阴山</a></li>
            <li><a href="http://www.126.com" alt="qi">岐王宅里寻常见,崔九堂前几度闻,正是江南好风景,落花时节又逢君</a></li>
            <li><a href="http://www.sina.com" class="du">杜甫</a></li>
            <li><a href="http://www.dudu.com" class="du">杜牧</a></li>
            <li><b>杜小月</b></li>
            <li><i>度蜜月</i></li>
            <li><a href="http://www.haha.com" id="feng">凤凰台上凤凰游,凤去台空江自流,吴宫花草埋幽径,晋代衣冠成古丘</a></li>
        </ul>
    </div>
</body>
</html>
测试页面数据
1.下载:pip install lxml 2.导包:from lxml import etree 3.将html文档或者xml文档转换成一个etree对象,而后调用对象中的方法查找指定的节点   2.1 本地文件:tree = etree.parse(文件名) tree.xpath("xpath表达式")   2.2 网络数据:tree = etree.HTML(网页内容字符串) tree.xpath("xpath表达式") 安装xpath插件在浏览器中对xpath表达式进行验证:能够在插件中直接执行xpath表达式 将xpath插件拖动到谷歌浏览器拓展程序(更多工具)中,安装成功 启动和关闭插件 ctrl + shift + x
from lxml import etree import requests url='http://www.haoduanzi.com/category-10_2.html' headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36', } url_content=requests.get(url,headers=headers).text #使用xpath对url_conten进行解析 #使用xpath解析从网络上获取的数据
tree=etree.HTML(url_content) #解析获取当页全部段子的标题
title_list=tree.xpath('//div[@class="log cate10 auth1"]/h3/a/text()') ele_div_list=tree.xpath('//div[@class="log cate10 auth1"]') text_list=[] #最终会存储12个段子的文本内容
for ele in ele_div_list: #段子的文本内容(是存放在list列表中)
    text_list=ele.xpath('./div[@class="cont"]//text()') #list列表中的文本内容所有提取到一个字符串中
    text_str=str(text_list) #字符串形式的文本内容防止到all_text列表中
 text_list.append(text_str) print(title_list) print(text_list)
项目需求:获取好段子中段子的内容和做者 http://www.haoduanzi.com
import requests from lxml import etree from fake_useragent import UserAgent import base64 import urllib.request url = 'http://jandan.net/ooxx' ua = UserAgent(verify_ssl=False,use_cache_server=False).random headers = { 'User-Agent':ua } page_text = requests.get(url=url,headers=headers).text #查看页面源码:发现全部图片的src值都是同样的。 #简单观察会发现每张图片加载都是经过jandan_load_img(this)这个js函数实现的。 #在该函数后面还有一个class值为img-hash的标签,里面存储的是一组hash值,该值就是加密后的img地址 #加密就是经过js函数实现的,因此分析js函数,获知加密方式,而后进行解密。 #经过抓包工具抓取起始url的数据包,在数据包中全局搜索js函数名(jandan_load_img),而后分析该函数实现加密的方式。 #在该js函数中发现有一个方法调用,该方法就是加密方式,对该方法进行搜索 #搜索到的方法中会发现base64和md5等字样,md5是不可逆的因此优先考虑使用base64解密 #print(page_text)
 tree = etree.HTML(page_text) #在抓包工具的数据包响应对象对应的页面中进行xpath的编写,而不是在浏览器页面中。 #获取了加密的图片url数据
imgCode_list = tree.xpath('//span[@class="img-hash"]/text()') imgUrl_list = [] for url in imgCode_list: #base64.b64decode(url)为byte类型,须要转成str
    img_url = 'http:'+base64.b64decode(url).decode() imgUrl_list.append(img_url) for url in imgUrl_list: filePath = url.split('/')[-1] urllib.request.urlretrieve(url=url,filename=filePath) print(filePath+'下载成功')
下载煎蛋网中的图片数据:http://jandan.net/ooxx

 

import requests from lxml import etree headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36' } url = 'http://pic.netbian.com/4kdongman/' response = requests.get(url=url,headers=headers) #手动设定响应数据的编码 # response.encoding = 'utf-8'
 page_text = response.text #数据解析(图片地址,图片名称)
tree = etree.HTML(page_text) li_list = tree.xpath('//div[@class="slist"]/ul/li') for li in li_list: #局部内容解析
    img_src = 'http://pic.netbian.com'+li.xpath('./a/img/@src')[0] img_name = li.xpath('./a/img/@alt')[0] #处理中文乱码的通用形式
    img_name = img_name.encode('iso-8859-1').decode('gbk') #返回图片二进制的数据.content
    img_data = requests.get(url=img_src,headers=headers).content img_path = './qiutuLibs/'+img_name+'.jpg' with open(img_path,'wb') as fp: fp.write(img_data) print(img_name,'下载成功!!!')
需求:下载彼岸图网的图片

 

爬取boss直聘里的招聘信息:职位,薪资,公司名称,岗位职责python

 

 

 

import requests from lxml import etree import json headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36' } url = 'https://www.zhipin.com/job_detail/?query=python%E7%88%AC%E8%99%AB&city=101010100&industry=&position=' page_text = requests.get(url=url,headers=headers).text #数据解析:jobName,salary,company,jobDesc
tree = etree.HTML(page_text) li_list = tree.xpath('//div[@class="job-list"]/ul/li') job_data_list = [] for li in li_list: job_name = li.xpath('.//div[@class="info-primary"]/h3/a/div/text()')[0] salary = li.xpath('.//div[@class="info-primary"]/h3/a/span/text()')[0] company = li.xpath('.//div[@class="company-text"]/h3/a/text()')[0] detail_url = 'https://www.zhipin.com'+li.xpath('.//div[@class="info-primary"]/h3/a/@href')[0] #详情页的页面源码数据
    detail_page_text = requests.get(url=detail_url,headers=headers).text detail_tree = etree.HTML(detail_page_text) job_desc = detail_tree.xpath('//*[@id="main"]/div[3]/div/div[2]/div[2]/div[1]/div//text()') job_desc = ''.join(job_desc) dic = { 'job_name':job_name, 'salary':salary, 'company':company, 'job_desc':job_desc } job_data_list.append(dic) fp = open('job.json','w',encoding='utf-8') json.dump(job_data_list,fp,ensure_ascii=False) fp.close() print('over')
需求:爬取boss直聘招聘信息

 

在全国空气质量历史数据查询网站上爬取全部城市名称linux

连接:https://www.aqistudy.cn/historydata/c++

多个xpath匹配('|')符号实现:city_list = tree.xpath('//div[@class="bottom"]/ul/li/a/text() | //div[@class="bottom"]/ul/div[2]/li/a/text()')ajax

import requests from lxml import etree headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36' } url = 'https://www.aqistudy.cn/historydata/' page_text = requests.get(url=url,headers=headers).text tree = etree.HTML(page_text) city_list = tree.xpath('//div[@class="bottom"]/ul/li/a/text() | //div[@class="bottom"]/ul/div[2]/li/a/text()') print(city_list) print(len(city_list)) #hot_city://div[@class="bottom"]/ul/li/a/text() #all_city://div[@class="bottom"]/ul/div[2]/li/a/text()
需求:爬取解析全国地区名称

 

三.BeautifulSoup解析

- 须要将pip源设置为国内源,阿里源、豆瓣源、网易源等 - windows (1)打开文件资源管理器(文件夹地址栏中) (2)地址栏上面输入 %appdata%3)在这里面新建一个文件夹 pip (4)在pip文件夹里面新建一个文件叫作 pip.ini ,内容写以下便可 [global] timeout = 6000 index-url = https://mirrors.aliyun.com/pypi/simple/ trusted-host = mirrors.aliyun.com - linux (1)cd ~2)mkdir ~/.pip (3)vi ~/.pip/pip.conf (4)编辑内容,和windows如出一辙 - 须要安装:pip install bs4 bs4在使用时候须要一个第三方库,把这个库也安装一下 pip install lxml
环境安装

 

基础使用 使用流程: - 导包:from bs4 import BeautifulSoup - 使用方式:能够将一个html文档,转化为BeautifulSoup对象,而后经过对象的方法或者属性去查找指定的节点内容 (1)转化本地文件: - soup = BeautifulSoup(open('本地文件'), 'lxml') (2)转化网络文件: - soup = BeautifulSoup('字符串类型或者字节类型', 'lxml') (3)打印soup对象显示内容为html文件中的内容 基础巩固: (1)根据标签名查找 - soup.a 只能找到第一个符合要求的标签 (2)获取属性 - soup.a.attrs 获取a全部的属性和属性值,返回一个字典 - soup.a.attrs['href'] 获取href属性 - soup.a['href'] 也可简写为这种形式 (3)获取内容 - soup.a.string - soup.a.text - soup.a.get_text() 【注意】若是标签还有标签,那么string获取到的结果为None,而其它两个,能够获取文本内容      string = xpath中的 /text()直系, text,get.test = xpath中的 //text()全部   (4)find:找到第一个符合要求的标签      - soup.find('a') 找到第一个符合要求的      - soup.find('a', title="xxx")      - soup.find('a', alt="xxx")      - soup.find('a', class_="xxx")      - soup.find('a', id="xxx")   (5)find_all:找到全部符合要求的标签      - soup.find_all('a')      - soup.find_all(['a','b']) 找到全部的a和b标签      - soup.find_all('a', limit=2) 限制前两个   (6)根据选择器选择指定的内容 
     - select:soup.select('#feng')      - 常见的选择器:标签选择器(a)、类选择器(.)、id选择器(#)、层级选择器      - 层级选择器: div .dudu #lala .meme .xixi 下面好多级 div > p > a > .lala 只能是下面一级   【注意】select选择器返回永远是列表,须要经过下标提取指定的对象       层级选择器中没法使用索引定位,会返回空,可是获得的列表是可使用索引定位的

 

案例演示:正则表达式

http://www.shicimingju.com/book/sanguoyanyi.html #!/usr/bin/env python # -*- coding:utf-8 -*-
import requests from bs4 import BeautifulSoup headers={ 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36', } def parse_content(url): #获取标题正文页数据
    page_text = requests.get(url,headers=headers).text soup = BeautifulSoup(page_text,'lxml') #解析得到标签
    ele = soup.find('div',class_='chapter_content') content = ele.text #获取标签中的数据值
    return content if __name__ == "__main__": url = 'http://www.shicimingju.com/book/sanguoyanyi.html' reponse = requests.get(url=url,headers=headers) page_text = reponse.text #建立soup对象
     soup = BeautifulSoup(page_text,'lxml') #解析数据
     a_eles = soup.select('.book-mulu > ul > li > a') print(a_eles) cap = 1
     for ele in a_eles: print('开始下载第%d章节'%cap) cap+=1 title = ele.string content_url = 'http://www.shicimingju.com'+ele['href'] content = parse_content(content_url) with open('./sanguo.txt','w') as fp: fp.write(title+":"+content+'\n\n\n\n\n') print('结束下载第%d章节'%cap)
需求:使用bs4实现将诗词名句网站中三国演义小说的每一章的内容爬去到本地磁盘进行存储
相关文章
相关标签/搜索