zoukankan      html  css  js  c++  java
  • 爬取校园新闻首页的新闻的详情,使用正则表达式,函数抽离

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

    # -*- coding:UTF-8 -*-
    # -*- author:deng -*-
    import requests
    import re
    from bs4 import BeautifulSoup
    from datetime import datetime
    
    newsurl='http://news.gzcc.cn/html/xiaoyuanxinwen/'
    res=requests.get(newsurl)
    res.encoding='utf-8'
    soup=BeautifulSoup(res.text,'html.parser')
    
    for i in soup.select('li'):
        if len(i.select('.news-list-title'))>0:
            x=i.select('.news-list-title')[0].text
            y=i.select('.news-list-description')[0].text
            z=i.select('.news-list-info')[0].text
            p = i.select('a')[0].attrs['href']
            print(x,y)
            print(z,p)

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

    for i in soup.select('li'):
        if len(i.select('.news-list-title'))>0:
            a=i.select('.news-list-title')[0].text
            b=i.select('.news-list-description')[0].text
            c=i.select('a')[0].attrs['href']
            read=requests.get(c)
            read.encoding='utf-8'
            soupSecond=BeautifulSoup(read.text,'html.parser')
            d=soupSecond.select('#content')[0].text
            print(soupSecond.select('.show-info')[0].text)

    3. 将字符串格式的发布时间转换成datetime类型

    4. 使用正则表达式取得新闻编号

    5. 生成点击次数的Request URL

    6. 获取点击次数

    7. 将456步骤定义成一个函数 def getClickCount(newsUrl):

    8. 将获取新闻详情的代码定义成一个函数 def getNewDetail(newsUrl):

    9. 尝试用使用正则表达式分析show info字符串,点击次数字符串。

    # 获取新闻点击次数
    def getNewsId(url):
        newsId = re.findall(r'\_(.*).html', newsUrl)[0][-4:]
        clickUrl = 'http://oa.gzcc.cn/api.php?op=count&id={}&modelid=80'.format(newsId)
        clickRes = requests.get(clickUrl)
        # 利用正则表达式获取新闻点击次数
        clickCount = int(re.search("hits').html('(.*)');", clickRes.text).group(1))
        return clickCount
    
    
    # 获取新闻细节
    def getNewsDetail(newsUrl):
        resd = requests.get(newsUrl)
        resd.encoding = 'utf-8'
        soupd = BeautifulSoup(resd.text, 'html.parser')
        content = soupd.select('#content')[0].text
        info = soupd.select('.show-info')[0].text
        # 调用getNewsId()获取点击次数
        count = getNewsId(newsUrl)
        print(info)
        # 识别时间格式
        date = re.search('(d{4}.d{2}.d{2}sd{2}.d{2}.d{2})', info).group(1)
        # 识别一个至三个数据
        author = re.search('作者:((.{3}s){1,3})', info).group(1)
        check = re.search('审核:((.{3}s){1,3})', info).group(1)
        sources = re.search('来源:((.{3}s){1,3})', info).group(1)
        # 用datetime将时间字符串转换为datetime类型
        dateTime = datetime.strptime(date, '%Y-%m-%d %H:%M:%S')
        # 利用format对字符串进行操作
        print('发布时间:{0}
    作者:{1}
    审核:{2}
    来源:{3}
    点击次数:{4}'.format(dateTime, author, check, sources, count))
        print(content)
    
    for new in soup.select('li'):
        if len(new.select('.news-list-title')) > 0:
            title = new.select('.news-list-title')[0].text
            description = new.select('.news-list-description')[0].text
            newsUrl = new.select('a')[0]['href']
    
            print('标题:{0}
    内容:{1}
    链接:{2}'.format(title, description, newsUrl))
            # 调用getNewsDetail()获取新闻详情
            getNewsDetail(newsUrl)
            break

  • 相关阅读:
    高精度计算
    高精度除以低精度
    P1258 小车问题
    POJ 2352 stars (树状数组入门经典!!!)
    HDU 3635 Dragon Balls(超级经典的带权并查集!!!新手入门)
    HDU 3938 Portal (离线并查集,此题思路很强!!!,得到所谓的距离很巧妙)
    POJ 1703 Find them, Catch them(确定元素归属集合的并查集)
    HDU Virtual Friends(超级经典的带权并查集)
    HDU 3047 Zjnu Stadium(带权并查集,难想到)
    HDU 3038 How Many Answers Are Wrong(带权并查集,真的很难想到是个并查集!!!)
  • 原文地址:https://www.cnblogs.com/dfq621/p/8762638.html
Copyright © 2011-2022 走看看