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

爬取校园新闻首页的新闻

时间:2018-04-03 23:53:52      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:post   log   sele   http   select   split   class   nts   pos   

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].text
        time =  news.select(‘.news-list-info span‘)[0].text
        # time = news.select(‘.news-list-info‘)[0].contents[0].text
        href = news.select(‘a‘)[0].attrs[‘href‘]
        print(time,title,href)
 
        # print(news)
 
        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].text
        dt = info.lstrip(‘发布时间:‘)[:19]
        # str = ‘2018-03-30 17:10:12‘
        dt2 = datetime.strptime(dt,‘%Y-%m-%d %H:%M:%S‘)
        print(dt2)
 
        
        if info.find(‘来源:‘) > 0:
            s =  info[info.find(‘来源:‘):].split()[0].lstrip(‘来源:‘)
            print(s)
        
        if info.find(‘作者:‘) > 0:
            l = info[info.find(‘作者:‘):].split()[0].lstrip(‘作者:‘)
            print(l)
        
        if info.find(‘摄影:‘) > 0:
            u = info[info.find(‘作者:‘):].split()[0].lstrip(‘作者:‘)
            print(u)

  

爬取校园新闻首页的新闻

标签:post   log   sele   http   select   split   class   nts   pos   

原文地址:https://www.cnblogs.com/xung/p/8711324.html

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