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


    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:
            print(news.select(".news-list-title"))
    
    for news in soup.select("li"):
        if len(news.select(".news-list-title")) > 0:
            t=news.select('.news-list-title')[0].text
            dt=news.select('.news-list-info')[0].contents[0].text
            a = news.select('a')[0].attrs['href']
            print(t,dt,a,'
    ')
    for news in soup.select("li"):
        if len(news.select(".news-list-title")) > 0:
            a = news.select('a')[0].attrs['href']#新闻链接
            t = news.select('.news-list-title')[0].text#标题
            res1=requests.get(a)
            res1.encoding='utf-8'
            soup1=BeautifulSoup(res1.text,'html.parser')
            dt=soup1.select('.show-info')[0].text#时间
            print(t,'
    ',dt,'
    ')
            print(soup1.select('#content')[0].text)#内容
            t1=dt.lstrip('发布时间:')[:19]
            i=dt.find('来源:')
            if i>0:
                s=dt[dt.find('来源:'):].split()[0].lstrip('来源:')
                a = dt[dt.find('作者:'):].split()[0].lstrip('作者:')
                p = dt[dt.find('摄影:'):].split()[0].lstrip('摄影:')
                print(t1,a,s,p)
            break
    str = '2018-03-30 17:10:12 '
    datetime.strptime(str,'%Y-%m-%d %H:%M:%S ')
    print('
    ',str)
  • 相关阅读:
    webmagic的使用学习
    redis在macOS上的安装及与springboot的整合使用
    Swagger-UI
    个人作业——软件工程实践总结&个人技术博客
    祝贺大野鸡喜提小黄衫一件
    软件评测(个人作业)
    结对第二次作业
    Springboot项目创建文件中相对路径问题
    二进制翻转
    欧拉降幂及广义欧拉降幂证明
  • 原文地址:https://www.cnblogs.com/qq974975766/p/8691955.html
Copyright © 2020-2023  润新知