• 理解爬虫原理


    作业来源:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE1/homework/2881
    1. 简单说明爬虫原理

      通过访问请求爬取网页上的数据

    2. 理解爬虫开发过程

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

      URL解析/DNS解析查找域名IP地址,网络连接发起HTTP请求,HTTP报文传输过程,服务器接收数据,服务器响应请求/MVC,服务器返回数据,客户端接收数据,浏览器加载/渲染页面,打印绘制输出所看到的网页。

    2).使用 requests 库抓取网站数据;
    requests.get(url) 获取校园新闻首页html代码
    import requests
    url ='http://news.gzcc.cn/html/xiaoyuanxinwen/'
    html = requests.get(url)
    html.encoding ='utf-8'
    print(html.text)
    

      

    2.了解网页

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>作业一</title>
    <stytle>
    
    </stytle>
    
    </head>
    <body>
        <form>
            <div id="name" class='content'><label>idddd</label><input type="text"></div>
            <div id="password" class='content'><label>passssword</label><input type="text"></div>
        </form>
    
        <button>德玛西亚</button>
        <button>拿锤子的人</button>
        <button>已经做出了选择</button>
        <button>猎杀陷入黑暗的</button>
    </body>
    </html>
    

      

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

    import requests
    from bs4 import BeautifulSoup
    
    from datetime import datetime
    
    url = 'http://news.gzcc.cn/html/2019/xiaoyuanxinwen_0320/11029.html'
    html = requests.get(url)
    html.encoding = 'utf-8'
    soup = BeautifulSoup(html.text,'html.parser')
    title = soup.select('.show-title')[0].text
    time = soup.select('.show-info')[0].text.split()[0:2]
    time = ' '.join(time)[5:]
    time = datetime.strptime(time,'%Y-%m-%d %H:%M:%S')
    add = str(soup.select('.show-info')[0].text.split()[4])
    writer = str(soup.select('.show-info')[0].text.split()[2])
    DingUrl="http://oa.gzcc.cn/api.php?op=count&id=11052&modelid=80"
    ding=int(requests.get(DingUrl).text.split('.html')[-1][2:-3])
    Ding = '点击次数:{}'.format(ding)
    Nr = soup.select('#content')[0].text.split()
    
    print(title)
    print('发布时间:{}'.format(time))
    print(add)
    print(writer)
    print(Ding)
    print(Nr[0]+'
    '+Nr[2]+'
    '+Nr[4]+'
    '+Nr[6])
    

      

  • 相关阅读:
    分享jQuery的常用技巧12招
    浅析淘宝数据魔方技术架构
    JavaScript的跨域共享的方法
    PHP实现QQ达人信息抓取
    Dreamweaver CS5.5试用小感和破解方法附下载地址
    ExtJS 4应用架构设计
    webkit webApp 开发技术要点总结
    用delphi编写ISAPI过滤器
    1020卡免费共享测试!
    一些有用的網站
  • 原文地址:https://www.cnblogs.com/Winslow-liujie/p/10638970.html
Copyright © 2020-2023  润新知