爬取校園新聞首頁的新聞

1. 用requests庫和BeautifulSoup庫,爬取校園新聞首頁新聞的標題、連接、正文。html

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:  #排除爲空的li
        time = news.select(".news-list-info")[0].contents[0].text
        title = news.select(".news-list-title")[0].text
        description = news.select(".news-list-description")[0].text
        a = news.select('a')[0].attrs['href']
        detail_res = requests.get(a)
        detail_res.encoding = "utf-8"
        detail_soup = BeautifulSoup(detail_res.text, "html.parser")
        print(detail_soup.select("#content")[0].text)#正文
        print(time, title, description, a)
        content = detail_soup.select("#content")[0].text
        info = detail_soup.select(".show-info")[0].text
        date_time = info.lstrip('發佈時間:')[:19]
        print(info)
        break

  

2. 分析字符串,獲取每篇新聞的發佈時間,做者,來源,攝影等信息。python

info = '發佈時間:2018-04-01 11:57:00      做者:陳流芳  審覈:權麟春  來源:馬克思主義學院      點擊:次'
detail_time = info.lstrip('發佈時間:')[:19]
sh = info[info.find("審覈"):].split()[0].lstrip('審覈:')
print(detail_time,sh)

  

3. 將其中的發佈時間由str轉換成datetime類型。url

# 獲取當前的時間
now_time = datetime.now();
now_time.year
# 將字符串轉化爲時間
print(datetime.strptime(date_time,"%Y-%m-%d %H:%M:%S"))
# 將時間轉化爲字符串
print(now_time.strftime('%Y\%m\%d'))

  

4. 將完整的代碼及運行結果截圖發佈在做業上。htm

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:  #排除爲空的li
        time = news.select(".news-list-info")[0].contents[0].text
        title = news.select(".news-list-title")[0].text
        description = news.select(".news-list-description")[0].text
        a = news.select('a')[0].attrs['href']
        detail_res = requests.get(a)
        detail_res.encoding = "utf-8"
        detail_soup = BeautifulSoup(detail_res.text, "html.parser")
        print(detail_soup.select("#content")[0].text)#正文
 
        print(time, title, description, a)
 
        content = detail_soup.select("#content")[0].text
        info = detail_soup.select(".show-info")[0].text
        date_time = info.lstrip('發佈時間:')[:19]
        print(info)
        break
 
info = '發佈時間:2018-04-01 11:57:00      做者:陳流芳  審覈:權麟春  來源:馬克思主義學院      點擊:次'
detail_time = info.lstrip('發佈時間:')[:19]
sh = info[info.find("審覈"):].split()[0].lstrip('審覈:')
print(detail_time,sh)
 
 
# # 多個名字查找做者
info1 = '發佈時間:2018-04-01 11:57:00      做者:陳流芳 許健傑  審覈:權麟春   來源:馬克思主義學院    點擊:次 '
info1 = info1[info1.find("做者"):info1.find('審覈:')].lstrip('做者:').split()[1]
print(info1)
 
# 獲取當前的時間
now_time = datetime.now();
now_time.year
 
# 將字符串轉化爲時間
print(datetime.strptime(date_time,"%Y-%m-%d %H:%M:%S"))
 
# 將時間轉化爲字符串
print(now_time.strftime('%Y\%m\%d'))
相關文章
相關標籤/搜索