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

    1. 简单说明爬虫原理

    a.向服务器发起请求

    b.获取响应内容

    c.解析内容

    d.保存内容

    2. 理解爬虫开发过程

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

    输入url,发送请求,通过网络连接,等待服务器相应返回数据,浏览器出现界面

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

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

    url='http://news.gzcc.cn/html/xiaoyuanxinwen'
    res = requests.get(url)

    3).了解网页

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

    html_sample = '
    <html>
    <body>
    <h1 id="title">Hello</h1>
    <a href="#" class="link"> This is link1</a >
    <a href="# link2" class="link" qao=123> This is link2</a >
    </body>
    </html> '

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

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

    select(选择器)定位数据

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

    a=soup.select('h1')[0].text
    print(a)

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

    for i in range(len(soup.select('.link'))):
        b=soup.select('.link')[i].text
    print(b)

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

    c=soup.select('#title')[0].text
    print(c)

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

    url='http://news.gzcc.cn/html/2019/xiaoyuanxinwen_0322/11042.html'
    res=requests.get(url)
    res.encoding='utf-8'
    soup1=BeautifulSoup(res.text,'html.parser')
    a=soup1.select('.show-title')[0].text
    b=soup1.select('.show-info')[0].text
    print(a,b)

  • 相关阅读:
    模块和包
    异常处理
    re模块下的的常用方法
    lambda匿名函数sorted排序函数filter过滤函数map映射函数
    内置函数
    生成器函数,迭代器
    网站架构伸缩性概论
    网站架构高可用小结
    Apache的三种工作模式
    HTTP协议
  • 原文地址:https://www.cnblogs.com/hzj111/p/10595008.html
Copyright © 2011-2022 走看看