• 爬取新闻列表


    1.获取单条新闻的#标题#链接#时间#来源#内容 #点击次数,并包装成一个函数。

    def shownews(url):
        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:
                time=news.select('.news-list-info')[0].span.text #时间
                title=news.select('.news-list-title')[0].contents[0] #标题
                url1=news.select('a')[0]['href'] #链接
                source=news.select('.news-list-info')[0].select('span')[1].text #来源
    
                resd=requests.get(url1)
                resd.encoding='utf-8'
                soupd=BeautifulSoup(resd.text,'html.parser')
                detail=soupd.select('.show-content')[0].text #内容
                getclick(url1)#点击数
                print( "标题:",title,"	时间:",time,"	链接:",url1,"来源:",source,"	点击数:",getclick(url1),"	内容:",detail)
                print("#################################################################################")

    2.获取一个新闻列表页的所有新闻的上述详情,并包装成一个函数。

    3.获取所有新闻列表页的网址,调用上述函数。

    4.完成所有校园新闻的爬取工作。

    5.完成自己所选其他主题相应数据的爬取工作。

    import requests
    import re
    from bs4 import BeautifulSoup
    url0='http://news.gzcc.cn/html/xiaoyuanxinwen/'
    res=requests.get(url0)
    res.encoding='utf-8'
    soup=BeautifulSoup(res.text,'html.parser')
    ##print(res.text)
    print("#################################################################################
    #######################################################")
    
    def getpage():
        lists=int(soup.select('.a1')[0].text.rstrip(""))
        page=lists//10+1
        return page
    
    def getclick(url2):
        id=re.search('_(.*).html',url2).group(1).split("/")[1]
        url3=('http://oa.gzcc.cn/api.php?op=count&id={}&modelid=80'.format(id))
        click=int(requests.get(url3).text.split('.')[-1].lstrip(".html('").rstrip("');"))
        return click
    def shownews(url):
        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:
                time=news.select('.news-list-info')[0].span.text #时间
                title=news.select('.news-list-title')[0].contents[0] #标题
                url1=news.select('a')[0]['href'] #链接
                source=news.select('.news-list-info')[0].select('span')[1].text #来源
    
                resd=requests.get(url1)
                resd.encoding='utf-8'
                soupd=BeautifulSoup(resd.text,'html.parser')
                detail=soupd.select('.show-content')[0].text #内容
                getclick(url1)#点击数
                print( "标题:",title,"	时间:",time,"	链接:",url1,"来源:",source,"	点击数:",getclick(url1),"	内容:",detail)
                print("#################################################################################")
    
    shownews(url0)
    
    for i in range(2,getpage()+1):
        url4=('http://news.gzcc.cn/html/xiaoyuanxinwen/{}.html'.format(i))
        shownews(url4)

    结果:

  • 相关阅读:
    大神没事接个活!
    帝国cms 后台账户密码都对,提示你还未登录!
    帝国cms 反馈时提示此信息反馈不存在
    帝国cms e:loop 文章编号标签
    eyoucms 首页以及列表页内容调用标题、关键词、描述
    eyoucms 点击下拉分类,没有分类的不切换代码
    eyoucms class类样式切换写法
    eyoucms if 判断显示或者隐藏
    eyoucms 调用搜索代码
    eyoucms 获取单页内容的代码
  • 原文地址:https://www.cnblogs.com/huanglinxin/p/7650945.html
Copyright © 2020-2023  润新知