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

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

    1. 简单说明爬虫原理

     通过程序模拟浏览器请求站点的行为,把站点返回的HTML代码/JSON数据/二进制数据(图片、视频) 爬到本地,进而提取自己需要的数据,存放起来使用

    2. 理解爬虫开发过程

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

     Web浏览器提交请求后,通过HTTP协议传送给Web服务器。Web服务器接到后, 进行事务处理,处理结果又通过HTTP传回给Web浏览器,从而在Web浏览器上显示出所请求的页面。 

    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元素

    soups = BeautifulSoup(html.text,'html.parser')
    soups.a  # a标签节点 
    soups.select('a') # a标签节点 
    soups.select('#id') # id=title节点
    soups.select('.class') # class=class节点

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

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

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

    import requests
    from bs4 import BeautifulSoup
    
    from datetime import datetime
    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')
    Bt = soup.select('.show-title')[0].text
    FbSj = soup.select('.show-info')[0].text.split()[0:2]
    FbSj = ' '.join(FbSj)[5:]
    FbSj = datetime.strptime(FbSj,'%Y-%m-%d %H:%M:%S')
    FbDw = str(soup.select('.show-info')[0].text.split()[4])
    Zz = str(soup.select('.show-info')[0].text.split()[2])
    DjCsUrl="http://oa.gzcc.cn/api.php?op=count&id=11052&modelid=80"
    djcs=int(requests.get(DjCsUrl).text.split('.html')[-1][2:-3])
    DjCs = '点击次数:{}'.format(djcs)
    Nr = soup.select('#content')[0].text.split()
    
    print(Bt)
    print('发布时间:{}'.format(FbSj))
    print(FbDw)
    print(Zz)
    print(DjCs)
    print(Nr[0]+'
    '+Nr[2]+'
    '+Nr[4]+'
    '+Nr[6])

  • 相关阅读:
    paper:Exploiting Query Reformulations for Web Search Result Diversification
    Z3
    IDA pro 类型参考
    Angr包含什么
    Gcc编译选项
    Angr
    一张图系列之PLT-GOT
    ELF头文件
    BROP_轮子
    Pwn_
  • 原文地址:https://www.cnblogs.com/Mram/p/10628765.html
Copyright © 2011-2022 走看看