zoukankan      html  css  js  c++  java
  • 爬取新闻列表 25

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

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

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

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

    复制代码
    import requests
    import re
    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')
    
    def getclicks(current_urls):
        id = re.search("_(.*).html",current_urls).groups()[0].split('/')[1]
        click =int(requests.get('http://oa.gzcc.cn/api.php?op=count&id={}&modelid=80'.format(id)).text.split('.')[-1].lstrip("html('").rstrip("');"))
        return click
    #每一页文章明细
    def getfirstpages(url24):
        res=requests.get(url24)
        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 
            time=news.select('.news-list-info')[0].contents[0].text#时间
            url1=news.select('a')[0]['href']#url
            bumen=news.select('.news-list-info')[0].contents[1].text#院系
            description=news.select('.news-list-description')[0].text#明细(文章)
    
            resd=requests.get(url1)
            resd.encoding='utf-8'
            soupd=BeautifulSoup(resd.text,'html.parser')
            detail = soupd.select('.show-content')[0].text #内容
            count = getclicks(url1)#调用函数
            #re.match('http://news.gzcc.cn/html/2017/xiaoyuanxinwen_1001/(.*).html',url).groups()
            
            print(title,count)
            #print(detail)
            #break
    n =int(soup.select('.a1')[0].text.rstrip('条'))
    number = n // 10 + 1
    for i in range(2,number+1):
        url01='http://news.gzcc.cn/html/xiaoyuanxinwen/{}.html'.format(i)
        getfirstpages(url01)
    复制代码

    复制代码
    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:
            title=news.select('.news-list-title')[0].text 
            time=news.select('.news-list-info')[0].contents[0].text#时间
            url1=news.select('a')[0]['href']#url
            bumen=news.select('.news-list-info')[0].contents[1].text
            description=news.select('.news-list-description')[0].text 
    
            resd=requests.get(url1)
            resd.encoding='utf-8'
            soupd=BeautifulSoup(resd.text,'html.parser')
            detail = soupd.select('.show-content')[0].text
    
            print(detail)
            break
    复制代码

    复制代码
    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:
            title=news.select('.news-list-title')[0].text 
            time=news.select('.news-list-info')[0].contents[0].text#时间
            url1=news.select('a')[0]['href']#url
            bumen=news.select('.news-list-info')[0].contents[1].text
            description=news.select('.news-list-description')[0].text 
    
            resd=requests.get(url1)
            resd.encoding='utf-8'
            soupd=BeautifulSoup(resd.text,'html.parser')
            detail = soupd.select('.show-content')[0].text #内容
    
            click = requests.get('http://oa.gzcc.cn/api.php?op=count&id=8249&modelid=80').text.split('.')[-1].lstrip("html('").rstrip("');")
            
            print(click)
            print(detail)
            break
    复制代码

  • 相关阅读:
    MongoDB数据创建与使用
    python库安装方法及下载依赖库
    java开发基础知识学习
    wifi破解基础及工具的使用
    Markdonw基本语法学习
    toj 4353 Estimation(树状数组+二分查找)
    POJ 1694 An Old Stone Game【递归+排序】
    POJ 2092 Grandpa is Famous【水---找出现第二多的数】
    POJ 2993 Emag eht htiw Em Pleh【模拟画棋盘】
    POJ 1068 Parencodings【水模拟--数括号】
  • 原文地址:https://www.cnblogs.com/kinoko/p/7684996.html
Copyright © 2011-2022 走看看