• 爬取校园新闻首页的新闻


    import requests
    from bs4 import BeautifulSoup
    from datetime import datetime
    
    res = requests.get('http://news.gzcc.cn/html/xiaoyuanxinwen/')
    res.encoding = 'utf-8'
    soup = BeautifulSoup(res.text, 'html.parser')
    for news in soup.select('li'):
        if len(news.select('.news-list-title'))>0:
            #d = news.select('.news-list-info')[0].contents[0].text
            t = news.select('.news-list-title')[0].text #标题
            m = news.select('.news-list-description')[0].text #内容
            a = news.select('a')[0].attrs['href']#链接
    
            print(t,a,m)
    
            resd = requests.get(a)
            resd.encoding = 'utf-8'
            soupd = BeautifulSoup(resd.text, 'html.parser')
    
            c = soupd.select('#content')[0].text
            print(c) #正文
            info = soupd.select('.show-info')[0].text
            print(info)
            dt = info.lstrip('发布时间:')[:19]
            dati = datetime.strptime(dt,'%Y-%m-%d %H:%M:%S')
            sh = info[info.find('来源:'):].split()[0].lstrip('来源:')
            wr = info[info.find('作者:'):info.find('审核:')].lstrip('作者:').split()[0]
            ph = info[info.find('摄影:'):].split()[0].lstrip('摄影:')
            print(dati,wr,sh,ph)
            break
    

  • 相关阅读:
    mysql分表技术(学习心得)
    Linux下搭建DNS服务器
    php简单单例模式
    JQuery的ajaxFileUpload图片上传初试
    Binary Tree Level Order Traversal II
    Remove Element
    Symmetric Tree
    Balanced Binary Tree
    Power of Two
    Merge Two Sorted Lists
  • 原文地址:https://www.cnblogs.com/cgq520/p/8710038.html
Copyright © 2020-2023  润新知