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
            href = new.select('a')[0].attrs['href']
            text = new.select('.news-list-description')[0].text
            print(title)
            print(href)
            print(text+'
    ')

     图片只展示部分

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

    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
            href = new.select('a')[0].attrs['href']
            text = new.select('.news-list-description')[0].text
            res = requests.get(href)
            res.encoding = 'utf-8'
            soup = BeautifulSoup(res.text, 'html.parser')
            info = soup.select('.show-info')[0].text
            content = info.split()
            time = content[0].lstrip('发布时间:') + " " + content[1]
            author = content[2].lstrip('作者:')
            check = content[3].lstrip('审核:')
            source = content[4].lstrip("来源:")
            print(time, author, check, source)

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

    from datetime import datetime
    time = '2018-04-04 05:20:34'
    a = datetime.strptime(time, '%Y-%m-%d %H:%M:%S')
    print(type(a))
    print(a, a.strftime('%Y/%m/%d'))

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

  • 相关阅读:
    菜刀失效以后得到web的方法
    mimikatz使用
    hydra使用教程
    web安全学习-攻击会话管理
    web安全学习-验证机制存在的问题
    web安全学习-绕过客户端限制
    基于MaxCompute/Dataworks实现数据仓库管理与全链路数据体系
    tensorflow学习|基于TF-IDF的垃圾短信预测
    WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform...
    CentOS7下安装CDH,clouderamanager,hadoop
  • 原文地址:https://www.cnblogs.com/REGzjm85/p/8719276.html
Copyright © 2011-2022 走看看