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:
            print(news.select(".news-list-title"))
    
    for news in soup.select("li"):
        if len(news.select(".news-list-title")) > 0:
            t=news.select('.news-list-title')[0].text
            dt=news.select('.news-list-info')[0].contents[0].text
            a = news.select('a')[0].attrs['href']
            print(t,dt,a,'
    ')
    for news in soup.select("li"):
        if len(news.select(".news-list-title")) > 0:
            a = news.select('a')[0].attrs['href']#新闻链接
            t = news.select('.news-list-title')[0].text#标题
            res1=requests.get(a)
            res1.encoding='utf-8'
            soup1=BeautifulSoup(res1.text,'html.parser')
            dt=soup1.select('.show-info')[0].text#时间
            print(t,'
    ',dt,'
    ')
            print(soup1.select('#content')[0].text)#内容
            t1=dt.lstrip('发布时间:')[:19]
            i=dt.find('摄影:')
            s=dt[dt.find('来源:'):].split()[0].lstrip('来源:')
            a = dt[dt.find('作者:'):].split()[0].lstrip('作者:')
            if i>0:
                p = dt[dt.find('摄影:'):].split()[0].lstrip('摄影:')
                print(t1,a,s,p)
            break
    str = '2018-03-30 17:10:12 '
    datetime.strptime(str,'%Y-%m-%d %H:%M:%S ')
    print('
    ',str)
  • 相关阅读:
    Android存储数据方式(转)
    Android实现双进程守护 (转)
    Android DOM、SAX、Pull解析XML(转)
    TCP/IP和Socket的关系(转)
    Socket通信原理和实践
    [转]Android中Intent传递对象的两种方法(Serializable,Parcelable)
    内存堆和栈的区别
    hdu 1754 线段树
    hdu 1166 线段树
    zoj 3686 线段树
  • 原文地址:https://www.cnblogs.com/TopHin/p/8697427.html
Copyright © 2011-2022 走看看