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

获取全部校园新闻

时间:2018-04-10 13:29:50      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:tle   one   else   cli   对象   coding   ext   href   html   

1.取出一个新闻列表页的全部新闻 包装成函数。

2.获取总的新闻篇数,算出新闻总页数。

3.获取全部新闻列表页的全部新闻详情。

import requests
import string
import re
from datetime import  datetime
newsurl=http://news.gzcc.cn/html/xiaoyuanxinwen/
res = requests.get(newsurl) #返回response对象
res.encoding=utf-8
from bs4 import BeautifulSoup
soup = BeautifulSoup(res.text,html.parser)

def getClickCount(newsUrl):
    newId=re.search(\_(.*).html,newsUrl).group(1).split(/)[1]
    clickUrl=http://oa.gzcc.cn/api.php?op=count&id={}modelid=80.format(newId)
    return(int(requests.get(clickUrl).text.split(.html)[-1].lstrip("(‘").rstrip("‘);")))



def getNewsDetail(newsurl):
    resd=requests.get(newsurl)
    resd.encoding=utf-8
    soupd=BeautifulSoup(resd.text,html.parser)
    title=soupd.select(.show-title)[0].text
    info=soupd.select(show-info)[0].text
    dt=datetime.strptime(info.lstrip(发布时间:)[0:19],%Y-%m-%d %H:%M:%S)
    if info.find(来源)>0:
        source=info[info.find(来源:):].split()[0].lstrip(来源:)
    else:
        source=none
    content=soupd.select(.show-content)[0].text.strip()
    click=getClickCount(newsurl)
    print(click,title,newsurl,source,dt)

def getListPage(listPageUrl):
    res=requests.get(listPageUrl)
    res.encoding=utf-8
    soup=BeautifulSoup(res.text,html.parser)
    for news in soup.select(li):
        if len(news.select(.news-list-title))>0:
            a=news.select(a)[0].attrs[href]
            getNewsDetail(a)



resn=requests.get(http://news.gzcc.cn/html/xiaoyuanxinwen/)
resn.encoding=utf-8
soupn=BeautifulSoup(resn.text,html.parser)
n=int(soupn.select(.a1)[0].text.rstrip())

for i in range(n,n+1):
    pageUrl=http://news.gzcc.cn/html/xiaoyuanxinwen/[].html.format(i)
    getListPage(pageUrl)

 

获取全部校园新闻

标签:tle   one   else   cli   对象   coding   ext   href   html   

原文地址:https://www.cnblogs.com/swxvico/p/8777892.html

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