zoukankan      html  css  js  c++  java
  • 理解爬虫原理

    1. 简单说明爬虫原理

    2. 理解爬虫开发过程

    1).简要说明浏览器工作原理;

    2).使用 requests 库抓取网站数据;

    requests.get(url) 获取校园新闻首页html代码

    3).了解网页

    写一个简单的html文件,包含多个标签,类,id

    4).使用 Beautiful Soup 解析网页;

    通过BeautifulSoup(html_sample,'html.parser')把上述html文件解析成DOM Tree

    select(选择器)定位数据

    找出含有特定标签的html元素

    找出含有特定类名的html元素

    找出含有特定id名的html元素

    3.提取一篇校园新闻的标题、发布时间、发布单位

    url = 'http://news.gzcc.cn/html/2019/xiaoyuanxinwen_0320/11029.html'

    主要代码:

    # -*- coding: utf-8 -*- 
    import requests
    from bs4 import BeautifulSoup
    import lxml
    res=requests.get("http://news.gzcc.cn/html/xiaoyuanxinwen/")
    #print(res.encoding)                       #获取当前的编码
    res.encoding = 'utf-8' 
    
    html=res.text
    soup = BeautifulSoup(html,"lxml")
    print(soup.prettify())
    
    
    res2=requests.get("http://news.gzcc.cn/html/2019/xiaoyuanxinwen_0320/11029.html")
    #print(res2.encoding)                       #获取当前的编码
    res2.encoding = 'utf-8'
    
    html=res2.text
    soup = BeautifulSoup(html,"lxml")
    print("标题:"+soup.select(".show-title")[0].text)
    print(soup.select(".show-info")[0].text)
    

      

  • 相关阅读:
    everything is nothing
    基础算法
    OC 优化目录
    iOS 更改启动视图
    单例--iOS
    OC-Objection 学习笔记之一:简单的开始
    iOS 类库列表
    IOS 上线问题
    OC强弱引用的使用规则
    设置桌面图标
  • 原文地址:https://www.cnblogs.com/wzh1997/p/10598643.html
Copyright © 2011-2022 走看看