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
    复制代码

  • 相关阅读:
    洛谷 P2922 [USACO08DEC]秘密消息Secret Message
    HDU 1542 Atlantis
    洛谷 P2146 软件包管理器
    rabbitmq
    POJ——T2446 Chessboard
    洛谷—— P3375 【模板】KMP字符串匹配
    洛谷——P3370 【模板】字符串哈希
    POJ——T1860 Currency Exchange
    洛谷—— P3386 【模板】二分图匹配
    python(1)- 初识python
  • 原文地址:https://www.cnblogs.com/kinoko/p/7684996.html
Copyright © 2011-2022 走看看