爬取博客列表

import requests
import re
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')

n=int(soup.select('.a1')[0].text.rstrip('条'))
pages=n//10+1
for i in range(2,pages+1):
    pagesurl='http://news.gzcc.cn/html/xiaoyuanxinwen/{}.html'.format(i)
    print(pagesurl)

import requests
import re
from bs4 import BeautifulSoup
def getclick(newurl):    #函数:采用匹配方式,动态获取每一文章点击次数
    id=re.search('_(.*).html',newurl).group(1).split('/')[1]
    clickurl='http://oa.gzcc.cn/api.php?op=count&id={}&modelid=80'.format(id)
    click=int(requests.get('http://oa.gzcc.cn/api.php?op=count&id=4662&modelid=80').text.split('.')[-1].lstrip("html('").rstrip("');"))
    return click

def getonepages(eveylisturl):  #函数:每一页的每一文章
    res=requests.get(eveylisturl)
    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#时间
            bumen=news.select('.news-list-info')[0].contents[1].text#院系
            #txt=news.select('.news-list-description')[0].text #正文

            resd=requests.get(url)
            resd.encoding='utf-8'
            soupd=BeautifulSoup(resd.text,'html.parser')
            detail=soupd.select('.show-content')[0].text #获取正文
            count=getclick(url)      #调用函数获得文章的点击次数
  
            print(title,count)

gzcc='http://news.gzcc.cn/html/xiaoyuanxinwen/'  #函数开头
res=requests.get(gzcc)
res.encoding='utf-8'
soup=BeautifulSoup(res.text,'html.parser')

        
     
n=int(soup.select('.a1')[0].text.rstrip('条'))
pages=n//10+1                    #计算多少条多少页
for i in range(2,5):
    pagesurl='http://news.gzcc.cn/html/xiaoyuanxinwen/{}.html'.format(i)
    getonepages(pagesurl)

  

感悟信仰力量,加强信仰教育——我校举行信仰教育研习班开班典礼 6195
杨文轩教授入选中国哲学社会科学最有影响力学者 6196
我校军训受到人民日报等媒体广泛关注 6197
责任信念记心头 扬帆起航正当时——我校举行2017级新生军训总结表彰大会 6198
英国赫尔大学商学院副院长Mr. Lynne Brrow一行来我校访问交流 6199
广东省教育厅高教处领导莅临我校调研 6200
广东省教育厅交流合作处领导莅临我校考察 6201
杨文轩主持召开广东省民办教育协会常务会议 6202
我校开展消防安全演练教育活动 6203
我校与美国内布拉斯加大学奥马哈分校联合举办开学典礼 6204
校领导看望慰问军训师生及教官 6205

原文地址:https://www.cnblogs.com/money1995/p/7684976.html