• 爬虫基础


    爬虫基础

    • 什么是爬虫

      • 通过编写程序模拟浏览器上网,然后让其去互联网上爬取数据的过程
    • 爬虫的分类

      • 通用爬虫:爬取一整张页面源码数据
      • 聚焦爬虫:爬取页面中指定的局部数据
      • 增量式爬虫:监测网站数据更新的情况.爬取的就是网站中最新更新出来的数据
    • requests

      • 作用
        • 模拟浏览器发请求
      • get/post参数:
        • url,
        • params/data,
        • headers
      • 编码流程:
        • 确定要爬取的数据是否是动态加载的
        • 指定url
        • 发起请求
        • 获取响应数据
        • 持久化存储
      • get/post返回值:响应对象response
        • text:字符串形式的响应数据
        • json():返回的是标准的json串
        • content:二进制形式的响应数据
        • encoding:响应数据的编码
    • 反爬机制

      • 门户网站通过相应的策略和技术手段,防止爬虫程序进行网站数据的爬取
    • 反反爬策略

    • 爬虫程序通过相应的策略和技术手段,破解门户网站的反爬虫手段,从而爬取到相应的数据

    • 第一个反爬机制

      • robots.txt协议
        • User-agent http的请求载体的身份标识
    • 编码流程:

      • 指定url
      • 发起请求
      • 获取响应数据
      • 持久化存储
    • jupyter快捷键

      启动:jupyter notebook
      插入:cell:a,b
      删除:x或者dd
      dell模式切换:m y
      执行cell:shift+enter
      tab:代码补全
      打开帮助文档:shift+tab
      
      
    • 爬取搜狗主页案例

      #1.指定url
      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)
      
    • 动态爬取搜狗搜索主页(简易的网页采集器)

      #User-Agent:请求载体的身份标识
      #UA检测:门户网站的服务器端会检测每一个请求的UA,如果检测到请求的UA为爬虫程序,则请求失败
      #UA伪装:
      #简易的网页采集器
      wd = input('enter a word:')
      url = 'https://www.sogou.com/web'
      #将请求参数设定成动态的
      param = {
          "query":wd
      }
      #UA伪装
      headers = {
          'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36'
      }
      response = requests.get(url=url,params=param,headers=headers)
      #手动设置响应数据的编码,处理中文乱码
      response.encoding = 'utf-8'
      #text返回的是字符串形式的响应数据
      page_text = response.text
      filename = wd+'.html'
      with open(filename,'w',encoding='utf-8') as fp:
          fp.write(page_text)
          print(filename,'下载成功')
      
    • 爬取肯德基餐厅位置信息

      #爬取肯德基餐厅位置信息
      
      url = 'http://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx?op=keyword'   
      city = input('enter a city name')
      for pageIndex in range(1,9):
          data = {
              "cname": "",
              "pid": "",
              "keyword": city,
              "pageIndex": pageIndex,
              "pageSize": "10",
          }
          headers = {
              'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36'
          }
          response = requests.post(url=url,data=data,headers=headers)
          #json() 返回的事一个json对象类型
          page_text = response.json()
      #     print(page_text)
          fp = open('./kfc.txt','a+',encoding='utf-8')
          kfc_dic = {}
          for dic in page_text['Table1']:
              kfc_dic[dic['storeName']+'餐厅'] = dic['addressDetail']
          fp.write(str(kfc_dic))
          fp.close()
      
    • 爬取豆瓣排行榜电影的信息

      import requests
      url = 'https://movie.douban.com/j/chart/top_list'
      s = 1
      limit = 100
      headers = {
              'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36'
          }
      param = {
          "type": "5",
          "interval_id": "100:90",
          "action": "",
          "start": s,
          "limit": limit,
      }
      response = requests.get(url=url,headers=headers,params=param)
      page_text = response.json()
      print(page_text)
      
    • 动态加载的页面数据

      • 发现首页中的所有的企业数据都是动态加载出来的
      • 通过抓包工具捕获动态加载数据对应的数据包
      • 从上一步的数据包对应的响应数据中提取到企业的相关信息(ID)
      • 通过分析每一家企业详情页的url发现,所有的详情页的url的域名都是一样的,只有id不同
      • 可以通过获取每一家企业的id结合着固定的域名拼接成详情页的url
      • 发现详情页中的企业信息是动态加载出来的,所以需要通过抓包工具数据包的捕获
      • 可以根据固定的url结合着不同的请求参数组合成企业详情数据对应的url
      • 对url发起请求就可以获取企业详情数据了
    • 爬取药监局的所有企业信息

      import requests
      
      url = 'http://125.35.6.84:81/xk/itownet/portalAction.do?method=getXkzsList'
      
      headers = {
              'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36'
          }
      def func_list(f):
          for i in range(1,330):
              data = {
              "on": "true",
              "page": i,
              "pageSize": "15",
              "productName":" ",
              "conditionType": "1",
              "applyname":" ",
              "applysn":" "
          }
              try:
                  page_text = requests.post(url=url, data=data, headers=headers).json()
              except Exception as e:
                  print(e)
                  continue
              for dic in page_text['list']:
                  _id = dic['ID']
                  detail_url = 'http://125.35.6.84:81/xk/itownet/portalAction.do?method=getXkzsById'
                  data = {'id': _id}
                  detail_text = requests.post(url=detail_url, data=data, headers=headers).json()
                  print(str(detail_text))
                  f.write(str(detail_text))
      
      with open('./particulars.txt','w',encoding='utf-8') as f:
      
          func_list(f)
      
  • 相关阅读:
    Decrease (Judge ver.)
    Raising Modulo Numbers
    最短Hamilton路径
    64位整数乘法
    递归系列——数组和对象的相关递归
    函数内容新增——函数表达式
    数据结构和算法(一)——栈
    (转)jQuery中append(),prepend()与after(),before()的区别
    微信端的user-Agent
    less知识点总结(二)
  • 原文地址:https://www.cnblogs.com/Godisgirl/p/11006855.html
Copyright © 2020-2023  润新知