• 爬取所有校园新闻


    1. 获取单条新闻的#标题#链接#时间#来源#内容 #点击次数,并包装成一个函数。
      import requests
      import re
      from bs4 import BeautifulSoup
      from datetime import datetime
      news='http://news.gzcc.cn/html/xiaoyuanxinwen/'
      res=requests.get(news)
      res.encoding='utf-8'
      soup=BeautifulSoup(res.text,'html.parser')
      
      def getdetail(url):
          resd=requests.get(url)
          resd.encoding='utf-8'
          soup=BeautifulSoup(resd.text,'html.parser')
          return(soup.select('.show-content')[0].text)
      def getclick(newsurl):
          id=re.match('http://news.gzcc.cn/html/2017/xiaoyuanxinwen_(.*).html',newsurl).groups()[0].split('/')[1]
          clickurl=('http://oa.gzcc.cn/api.php?op=count&id={}&modelid=80').format(id)
          click=int(requests.get(clickurl).text.split('.')[-1].lstrip("html('").rstrip("');"))
          return(click)
          
      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')
              where=(news.select('.news-list-info')[0].contents[1].text) #来源
              resd=requests.get(url)
              resd.encoding='utf-8'
              soup=BeautifulSoup(resd.text,'html.parser')
              detail=getdetail(url) #详情
              click=getclick(url)#点击次数
              print(title,url,time,dt,where,click)
      
              
              
    2. 获取一个新闻列表页的所有新闻的上述详情,并包装成一个函数。
      import requests
      import re
      from bs4 import BeautifulSoup
      from datetime import datetime
      news='http://news.gzcc.cn/html/xiaoyuanxinwen/'
      res=requests.get(news)
      res.encoding='utf-8'
      soup=BeautifulSoup(res.text,'html.parser')
      
      def getdetail(url):
          resd=requests.get(url)
          resd.encoding='utf-8'
          soup=BeautifulSoup(resd.text,'html.parser')
          return(soup.select('.show-content')[0].text)
      def getclick(newsurl):
          id=re.search('_(.*).html',newsurl).group(1).split('/')[1]
          clickurl=('http://oa.gzcc.cn/api.php?op=count&id={}&modelid=80').format(id)
          click=int(requests.get(clickurl).text.split('.')[-1].lstrip("html('").rstrip("');"))
          return(click)
      def getonepage(listurl):
          resd=requests.get(listurl)
          resd.encoding='utf-8'
          soup=BeautifulSoup(resd.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 #标题
                  a=news.select('a')[0]['href'] #链接
                  time=(news.select('.news-list-info')[0].contents[0].text) #时间
                  dt=datetime.strptime(time,'%Y-%m-%d')
                  where=(news.select('.news-list-info')[0].contents[1].text) #来源
                  detail=getdetail(a) #详情
                  click=getclick(a)#点击次数
                  print(title,time,dt,a,where,click)
      getonepage('http://news.gzcc.cn/html/xiaoyuanxinwen/index.html')
      res=requests.get('http://news.gzcc.cn/html/xiaoyuanxinwen/')
      res.encoding='utf-8'
      soup=BeautifulSoup(res.text,'html.parser')
      page=int(soup.select('.a1')[0].text.rstrip(''))//10+1                 
      for i in range(2,page):
          gzccurl='http://news.gzcc.cn/html/xiaoyuanxinwen/{}.html'.format(i)
          getonepage(gzccurl)
              
  • 相关阅读:
    工作感悟(一)
    laydate组件选择时间段的判断
    Win10下免安装版JDK8环境变量配置
    IDEA中lombok插件的安装
    解决加载WEB页面时,由于JS文件引用过多影响页面打开速度的问题
    Windows环境下的MYSQL5.7配置文件定位
    MYSQL使用source命令,导入SQL文件
    MYSQL5.7生成列简介及创建
    MYSQL慢查询优化方法及优化原则
    批量提取文件夹下所有目录及文件名称
  • 原文地址:https://www.cnblogs.com/lcm1995/p/7657538.html
Copyright © 2020-2023  润新知