zoukankan      html  css  js  c++  java
  • 爬取校园新闻首页的新闻

    import requests
    from bs4 import BeautifulSoup
    from datetime import datetime
    
    url = "http://news.gzcc.cn/html/xiaoyuanxinwen/"
    res = requests.get(url)
    res.encoding = "utf-8"
    soup = BeautifulSoup(res.text,"html.parser")
    
    for news in soup.select('li'):
        if len(news.select('.news-list-title'))>0:
            title = news.select('.news-list-title')[0].get_text()
            time =  news.select('.news-list-info span')[0].get_text()
            href = news.select('a')[0].attrs['href']
            print(time,title,href)
    
    
            res1 = requests.get(href)
            res1.encoding = "utf-8"
            soup = BeautifulSoup(res1.text, "html.parser")
            con = soup.select('#content')[0].tedtxt
            info = soup.select('.show-info')[0].get_text()
            dt = info.lstrip('发布时间:')[:19]
            dt2 = datetime.strptime(dt,'%Y-%m-%d %H:%M:%S')
            print(dt2)
    
            i = info.find('来源:')
            if i>0:
                s =  info[info.find('来源:'):].split()[0].replace('来源:')
                print(s)
            a = info.find('作者:')
            if a > 0:
                l = info[info.find('作者:'):].split()[0].replace('作者:')
                print(l)
            y = info.find('摄影:')
            if y > 0:
                u = info[info.find('作者:'):].split()[0].replace('作者:')
                print(u)
    

      

  • 相关阅读:
    linux 查看父进程号
    gitlab
    诺基亚C6常识详解
    C#.net书籍列表
    表链接
    Limu:JavaScript的那些书(转载)
    Oracle 多行记录合并/连接/聚合字符串的几种方法
    关于有锁iPhone的常识(转载)
    not in与not exists性能比较
    多表连接查询
  • 原文地址:https://www.cnblogs.com/piglet00/p/8706643.html
Copyright © 2011-2022 走看看