zoukankan      html  css  js  c++  java
  • 爬取校园网

    1. 用requests库和BeautifulSoup库,爬取校园新闻首页新闻的标题、链接、正文。

    import requests
    from bs4 import BeautifulSoup
    
    url = 'http://news.gzcc.cn/html/xiaoyuanxinwen/'
    res = requests.get(url)
    res.encoding = 'utf-8'
    soup = BeautifulSoup(res.text, 'html.parser')
    
    news = soup.select('li')
    for new in news:
        if len(new.select('.news-list-title')) > 0:
            title = new.select('.news-list-title')[0].text  # 标题
            aurl = new.select('a')[0].attrs['href']  # URL
            text = new.select('.news-list-description')[0].text #正文
            print(title+'
    '+text+'
    '+aurl+'
    ')
    

     运行结果:

    2. 分析字符串,获取每篇新闻的发布时间,作者,来源,摄影等信息。

    res2 = requests.get(aurl)
            res2.encoding = 'utf-8'
            soup2 = BeautifulSoup(res2.text, 'html.parser')
            info = soup2.select('.show-info')[0].text
            info = info.lstrip('发布时间:').rstrip('点击:次')
            # print(info)
            time = info[:info.find('作者')] # 发布时间
            author = info[info.find('作者:')+3:info.find('审核')] # 作者
            check = info[info.find('审核:')+3:info.find('来源')]  # 审核
            source = info[info.find("来源:")+3:info.find('摄影')] # 来源
            print(time, author, check, source,end="")
            if(info.find("摄影:")>0):
                photogra = info[info.find("摄影:")+3:] # 摄影
                print(photogra)
            else:
                print()
    

     运行结果:

    3. 将其中的发布时间由str转换成datetime类型。

    from datetime import datetime
    
    time = '2018-04-01 11:57:00'
    formart = datetime.strptime(time, '%Y-%m-%d %H:%M:%S')
    print(type(formart))
    print(formart, formart.strftime('%Y/%m/%d'))
    

     运行结果:

    4. 将完整的代码及运行结果截图发布在作业上。

    import requests
    from bs4 import BeautifulSoup
    
    url = 'http://news.gzcc.cn/html/xiaoyuanxinwen/'
    res = requests.get(url)
    res.encoding = 'utf-8'
    soup = BeautifulSoup(res.text, 'html.parser')
    
    news = soup.select('li')
    for new in news:
        if len(new.select('.news-list-title')) > 0:
            title = new.select('.news-list-title')[0].text  # 标题
            aurl = new.select('a')[0].attrs['href']  # URL
            text = new.select('.news-list-description')[0].text #正文
            print(title + '
    ' + text + '
    ' + aurl + '
    ')
    
            res2 = requests.get(aurl)
            res2.encoding = 'utf-8'
            soup2 = BeautifulSoup(res2.text, 'html.parser')
            info = soup2.select('.show-info')[0].text
            info = info.lstrip('发布时间:').rstrip('点击:次')
            # print(info)
            time = info[:info.find('作者')] # 发布时间
            author = info[info.find('作者:')+3:info.find('审核')] # 作者
            check = info[info.find('审核:')+3:info.find('来源')]  # 审核
            source = info[info.find("来源:")+3:info.find('摄影')] # 来源
            print(time, author, check, source,end="")
            if(info.find("摄影:")>0):
                photogra = info[info.find("摄影:")+3:] # 来源
                print(photogra)
            else:
                print()
    

      

      

  • 相关阅读:
    内网渗透笔记
    shift粘滞键后门创建/复原批处理
    通过Wireshark抓包进行Cookie劫持
    最全前端资源汇集 (持续整理中)
    HTML5教程之html 5 本地数据库(Web Sql Database)
    myslq 基本命令
    Node.js面试题:侧重后端应用与对Node核心的理解
    44个 Javascript 变态题解析 (上下)
    BAT及各大互联网公司前端笔试面试题--Html,Css篇
    Web前端面试题集锦
  • 原文地址:https://www.cnblogs.com/veol/p/8710051.html
Copyright © 2011-2022 走看看