代码改变世界

爬取校园新闻首页的新闻

2018-04-02 11:39  AAAAAAABE  阅读(133)  评论(0编辑  收藏  举报

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

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

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

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:
#         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)
#      ##

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
        a = news.select('a')[0].attrs['href']
        print(a)
        resd = requests.get(a)
        resd.encoding = 'utf-8'
        soupd = BeautifulSoup(resd.text, 'html.parser')
       # print(soupd.select('.show-info'))
       # print(soupd.select('#content'))
        d = soupd.select('#content')[0].text
        info = soupd.select('.show-info')[0].text
        print(info)
        dt = info.lstrip('发布时间:')[:19]
        print(dt)
        i = info.find('来源:')
        if i >0:
            s = info[info.find('来源:'):].split()[0].lstrip('来源:')
            print(s)

        z = info.find('作者:')
        if z > 0:
            z = info[info.find('作者:'):].split()[0].lstrip('作者:')
            print(z)

        y = info.find('摄影:')
        if y > 0:
            y = info[info.find('摄影:'):].split()[0].lstrip('摄影:')
            print(y)

        break

from datetime import datetime
        str ='2018-03-30 17:10:12'
        dt1 = datetime.strptime(str,'%Y-%M-%D %H:%M:%S').year
        now = datetime.now()
        type(now)
        now.strftime("%Y-%M-%D %H:%M:%S")