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()
    

      

      

  • 相关阅读:
    H
    饭卡(0 1背包)
    J
    H
    E
    3991: [SDOI2015]寻宝游戏
    CF 1051 F. The Shortest Statement
    CF 643 E. Bear and Destroying Subtrees
    4567: [Scoi2016]背单词
    4946: [Noi2017]蔬菜
  • 原文地址:https://www.cnblogs.com/veol/p/8710051.html
Copyright © 2011-2022 走看看