• 爬取校园新闻首页的新闻


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

    import requests
    from bs4 import BeautifulSoup
    
    res = requests.get('http://news.gzcc.cn/html/xiaoyuanxinwen/')
    res.encoding='utf-8'
    soup=BeautifulSoup(res.text,'html.parser')
    for news in soup.select('li'):
        if len(news.select('.news-list-title'))> 0:
            d=news.select('.news-list-info')[0].contents[0].text
            t=news.select('.news-list-title')[0].text
            ti=news.select('a')[0].attrs['href']
    
            resf=requests.get(ti)
            resf.encoding='utf-8'
            soupd=BeautifulSoup(resf.text,'html.parser')
            s=soupd.select('.show-info')[0].text
    
            print(t,ti)
            print(soupd.select('#content')[0].text)
    

     

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

    res2 = requests.get(ti)
            res2.encoding = 'utf-8'
            soup2 = BeautifulSoup(res2.text, 'html.parser')
            info = soup2.select('.show-info')[0].text
            info = info.lstrip('发布时间:').rstrip('点击:次')
            # print(info)
            time = info[:info.find('作者')] # 发布时间
            author = info[info.find('作者:')+3:info.find('审核')] # 作者
            check = info[info.find('审核:')+3:info.find('来源')]  # 审核
            source = info[info.find("来源:")+3:info.find('摄影')] # 来源
            print(time, author, check, source,end="")
            if(info.find("摄影:")>0):
                photogra = info[info.find("摄影:")+3:] # 摄影
                print(photogra)
            else:
                print()
    

      

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

    res3 = requests.get('http://news.gzcc.cn/html/xiaoyuanxinwen/')
    res3.encoding = 'utf-8'
    soup3 = BeautifulSoup(res3.text,'html.parser')
     
    for news in soup3.select('li'):
        if len(news.select('.news-list-title')) > 0:
     
            a = news.a.attrs['href']
            resq = requests.get(a)
            resq.encoding = 'utf-8'
            soupq = BeautifulSoup(resq.text,'html.parser')
            info = soupq.select('.show-info')[0].text
            dt = info.lstrip('发布时间:')[:19]
            from datetime import datetime
            dt = info.lstrip('发布时间:')[:19]
            dati = datetime.strptime(dt, '%Y-%m-%d %H:%M:%S')
            print(dati, dati.strftime('%Y/%m/%d'))
    

      

    4. 将完整的代码及运行结果截图发布在作业上。

    import requests
    from bs4 import BeautifulSoup
    
    res = requests.get('http://news.gzcc.cn/html/xiaoyuanxinwen/')
    res.encoding='utf-8'
    soup=BeautifulSoup(res.text,'html.parser')
    for news in soup.select('li'):
        if len(news.select('.news-list-title'))> 0:
            d=news.select('.news-list-info')[0].contents[0].text
            t=news.select('.news-list-title')[0].text
            ti=news.select('a')[0].attrs['href']
    
            resf=requests.get(ti)
            resf.encoding='utf-8'
            soupd=BeautifulSoup(resf.text,'html.parser')
            s=soupd.select('.show-info')[0].text
    
            print(t,ti)
            print(soupd.select('#content')[0].text)
    
    
            res2 = requests.get(ti)
            res2.encoding = 'utf-8'
            soup2 = BeautifulSoup(res2.text, 'html.parser')
            info = soup2.select('.show-info')[0].text
            info = info.lstrip('发布时间:').rstrip('点击:次')
            
            time = info[:info.find('作者')]
            author = info[info.find('作者:')+3:info.find('审核')]
            check = info[info.find('审核:')+3:info.find('来源')]
            source = info[info.find("来源:")+3:info.find('摄影')]
            print(time, author, check, source,end="")
            if(info.find("摄影:")>0):
                photogra = info[info.find("摄影:")+3:]
                print(photogra)
            else:
                print()
    
    res3 = requests.get('http://news.gzcc.cn/html/xiaoyuanxinwen/')
    res3.encoding = 'utf-8'
    soup3 = BeautifulSoup(res3.text,'html.parser')
     
    for news in soup3.select('li'):
        if len(news.select('.news-list-title')) > 0:
     
            a = news.a.attrs['href']
            resq = requests.get(a)
            resq.encoding = 'utf-8'
            soupq = BeautifulSoup(resq.text,'html.parser')
            info = soupq.select('.show-info')[0].text
            dt = info.lstrip('发布时间:')[:19]
            from datetime import datetime
            dt = info.lstrip('发布时间:')[:19]
            dati = datetime.strptime(dt, '%Y-%m-%d %H:%M:%S')
            print(dati, dati.strftime('%Y/%m/%d'))
    

      

      

  • 相关阅读:
    【Linux】在Linux上,使用校园出校器拨号的一个脚本。
    【Android】编译CM10.1遇到的错误解决方案
    【Android】编译CM10遇到的错误解决方案
    【Android】CM在repo中使用local manifest
    一个网站的诞生 MagicDict开发总结1 [首页]
    我记录网站综合系统 1.6发布
    带有ToolTip的ListBox
    字符串的宽度
    .NET开发的文本编辑器,(又发明轮子了,VB代码,不喜误入)
    捕获输入内容
  • 原文地址:https://www.cnblogs.com/hkvbm/p/8709893.html
Copyright © 2020-2023  润新知