• 理解爬虫原理


     

    作业来源:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE1/homework/2881

    1. 简单说明爬虫原理

    2. 理解爬虫开发过程

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

    1)用户界面-地址栏,前进、后退,书签菜单。 
    2)浏览器引擎-在用户接和呈现引擎之间传送指令。 
    3)呈现引擎-显示请求的内容。 
    4)网络-网络调用,如http请求。 
    5)用户界面后端-绘制基本窗口小部件。 
    6)JavaScript解析器-解析和执行js代码。 
    7)数据存储-持久层,把需要的数据保存在硬盘上,如cookie. 

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

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

    html=requests.get('http://news.gzcc.cn/html/2019/xiaoyuanxinwen_0329/11095.html')

    3).了解网页

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

    <!DOCTYPE html>
    <html>
    <head> 
    <meta charset="utf-8"> 
    <title>gzcc(runoob.com)</title> 
    </head>
    <body>
    
    
    
    <div style="color:#0000FF" class=“1”>
      <p>这是第1个在 div 元素中的文本。</p>
    </div>
    
        <div style="color:#0000FF" class=“2”>
      <h2>这是第2个在 div 元素中的标题。</h3>
      <p>这是3个在 div 元素中的文本。</p>
    </div>
    
        <div style="color:#0000FF" class=“3”>
      <h3>这是4个在 div 元素中的文本。</h3>
      <p>这是5个在 div 元素中的文本。</p>
    </div>
        
        
    
    
    </body>
    </html>

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

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

    select(选择器)定位数据

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

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

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

    import requests
    import bs4
    from bs4 import BeautifulSoup
    from datetime import datetime
    html=requests.get('http://news.gzcc.cn/html/2019/xiaoyuanxinwen_0329/11095.html')
    html.encoding='utf-8'#去掉这句则乱码,加上则正常显示,其中utf-8是根据网页源代码中设置的编码格式来指定的  
    print(html.text)
    print('---------------------------------------------------------------------------------------------------------')
    t1=html.text
    soups = BeautifulSoup(t1,'html.parser')
    a1 =soups.a
    a = soups.select('a')
    h = soups.select('h1')
    t = soups.select('#title')
    l = soups.select('.link')
    print(a)

    3.提取一篇校园新闻的标题、发布时间、发布单位、作者、点击次数、内容等信息

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

    要求发布时间为datetime类型,点击次数为数值型,其它是字符串类型。

    import requests
    import bs4
    from bs4 import BeautifulSoup
    from datetime import datetime
    html=requests.get('http://news.gzcc.cn/html/2019/xiaoyuanxinwen_0329/11095.html')
    html.encoding='utf-8'#去掉这句则乱码,加上则正常显示,其中utf-8是根据网页源代码中设置的编码格式来指定的
    soups = BeautifulSoup(t1,'html.parser')
    ti=soups.select('.show-title')[0].text
    info=soups.select('.show-info')[0].text.split()#info此时是列表
    content=soups.select('#content')#正文存入content
    publish_date=info[0].split('发布时间:')[1]#以发布时间隔开成为数组,选取第二个元素
    click = BeautifulSoup(t1.split(".html")[-1].split("'")[1],'html.parser')
    date=datetime.strptime(publish_date+' '+info[1],'%Y-%m-%d %H:%M:%S')
    zuozhe =info[2]
    shenhe=info[3]
    laiyuan=info[4]
    sheying=info[5]

    print('标题:'+ti)
    print('发布时间:'+str(date))
    print(zuozhe+' '+shenhe+' '+laiyuan+' '+sheying)
    print("点击次数:"+str(click))
    print("内容:"+content[0].text)

     

  • 相关阅读:
    批处理处理oracle数据库脚本导入
    Log4Net 配置说明
    MSSQL和oracle数据通讯
    计算字符串中每种字符出现的次数[Dictionary<char,int>泛型集合用法]
    newagg新蛋笔试题
    Oracle数据库切换SQLServer数据库解决方案
    sqlserver 的事务和c#的事务
    SQL Server 2005 导出 数据脚本
    ASP.NET CMD WebShell
    Silverlight BUG
  • 原文地址:https://www.cnblogs.com/JunhanLin/p/10622909.html
Copyright © 2020-2023  润新知