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

爬取校园新闻首页的新闻

时间:2018-04-03 22:17:19      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:图片   image   爬取   首页   list   post   from   int   lis   

1. 用requests库和BeautifulSoup库,爬取校园新闻首页新闻的标题、链接、正文。

技术分享图片

2. 分析字符串,获取每篇新闻的发布时间,作者,来源,摄影等信息。

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:
        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(dt,t,a)#爬取校园新闻首页新闻的标题、链接
        res2 = requests.get(a)
        res2.encoding = ‘utf-8‘
        soup2 = BeautifulSoup(res2.text, ‘html.parser‘)
        te = soup2.select(‘#content‘)[0].text
        print(te)#爬取校园新闻首页新闻的正文

        ifd = soup2.select(‘.show-info‘)[0].text
        dt2 = ifd.lstrip(‘发布时间:‘)[:19]
        print(dt2)#获取每篇新闻的发布时间

        i = ifd.find(‘作者:‘)
        if i>0:
            s = ifd[ifd.find(‘作者:‘):].split()[0].lstrip(‘作者:‘)
            print(s)#获取每篇新闻的作者。

        q = ifd.find(‘来源:‘)
        if q > 0:
            b = ifd[ifd.find(‘来源:‘):].split()[0].lstrip(‘来源:‘)
            print(b)#获取每篇新闻的来源。

        c = ifd.find(‘摄影:‘)
        if c > 0:
            n = ifd[ifd.find(‘摄影:‘):].split()[0].lstrip(‘摄影:‘)
            print(n)#获取每篇新闻的摄影。

        dtn = datetime.strptime(dt2,‘%Y-%m-%d %H:%M:%S‘)#将其中的发布时间由str转换成datetime类型。
        print(dtn)
        break

3. 将其中的发布时间由str转换成datetime类型。

import requests
from bs4 import BeautifulSoup
from datetime import datetime

gzccurl = ‘http://news.gzcc.cn/html/xiaoyuanxinwen/‘
res = requests.get(gzccurl)
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#标题
        url = news.select(‘a‘)[0][‘href‘]#链接
        time = news.select(‘.news-list-info‘)[0].contents[0].text
        dt = datetime.strptime(time,‘%Y-%m-%d‘)
        source = news.select(‘.news-list-info‘)[0].contents[1].text#来源
        print(dt,‘\n‘,title,‘\n‘,url,‘\n‘,source,‘\n‘)

爬取校园新闻首页的新闻

标签:图片   image   爬取   首页   list   post   from   int   lis   

原文地址:https://www.cnblogs.com/byyl/p/8691824.html

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