码迷,mamicode.com
首页 > 其他好文 > 详细

爬取校园新闻首页的新闻

时间:2018-04-03 23:48:41      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:parse   rom   gpo   type   nts   div   attr   upd   html   

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

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
        a = news.select(‘a‘)[0].attrs[‘href‘]
        print("date = "+d, "text = "+t, "href = "+a)

  技术分享图片

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
        a = news.select(‘a‘)[0].attrs[‘href‘]
        # print("date = "+d, "text = "+t, "href = "+a)


        resd = requests.get(a)
        resd.encoding = ‘utf-8‘
        soupd = BeautifulSoup(resd.text, ‘html.parser‘)
        c = soupd.select(‘#content‘)[0].text
        info = soupd.select(".show-info")
        # print(info)
        print(info[0].text)

  技术分享图片

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
        a = news.select(‘a‘)[0].attrs[‘href‘]
        # print("date = "+d, "text = "+t, "href = "+a)


        resd = requests.get(a)
        resd.encoding = ‘utf-8‘
        soupd = BeautifulSoup(resd.text, ‘html.parser‘)
        c = soupd.select(‘#content‘)[0].text
        info = soupd.select(".show-info")
        # print(info)
        # print(info[0].text)
        #
        # info_text = ‘发布时间:2018-04-01 11:57:00      作者:陈流芳??审核:权麟春  来源:马克思主义学院      点击:次‘
        dt = info[0].text.lstrip(‘发布时间:‘)[:19]
        sh = info[0].text[info[0].text.find(‘审核:‘):].split()[0].lstrip(‘审核:‘)
        print(type(dt), dt)
        from datetime import datetime

        date_format = datetime.strptime(dt, ‘%Y-%m-%d %H:%M:%S‘)
        print(type(date_format), date_format)

  技术分享图片

 

爬取校园新闻首页的新闻

标签:parse   rom   gpo   type   nts   div   attr   upd   html   

原文地址:https://www.cnblogs.com/phoenlix/p/8711526.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!