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

    import requests
    from bs4 import BeautifulSoup
    from datetime import datetime
    import re
    url = 'http://news.gzcc.cn/html/xiaoyuanxinwen/'
    res = requests.get(url)
    res.encoding = 'utf-8'
    soup = BeautifulSoup(res.text, 'html.parser')
    
    
    def getClick(newsUrl):
        newId = re.search('\_(.*).html', newsUrl).group(1).split('/')[1]
        click = requests.get('http://oa.gzcc.cn/api.php?op=count&id={}&modelid=80'.format(newId))
        return click.text.split('.html')[-1].lstrip("('").rstrip("');")
    
    
    def getNews(newsUrl):
        f = re.search('作者:((.{3}s){1,3})', info).group(1)
        date = re.search('(d{4}.d{2}.d{2}sd{2}.d{2}.d{2})', info).group(1)
        i = re.search('审核:((.{3}s){1,3})', info).group(1)
        l = re.search('来源:((.{3}s){1,3})', info).group(1)
        m = re.search('摄影:((..{3}s){1,3})',info).group(1)
        count = getClick(newsUrl)
        dateTime = datetime.strptime(date, '%Y-%m-%d %H:%M:%S')
        print('发布时间:{0}'.format(dateTime))
        print('作者:{0}'.format(f))
        print('审核:{0}' .format(i))
        print('来源:{0}' .format(l))
        print('摄影:{0}' .format(m))
        print('点击:{0}' .format(count) + '')
    
    
    
    for news in soup.select('li'):
        if len(news.select('.news-list-title')) > 0:
            link = news.a.attrs['href']
            t = news.select('.news-list-title')[0].text
            d = news.select('.news-list-description')[0].text
            newsUrl = news.select('a')[0]['href']
            res = requests.get(newsUrl)
            res.encoding = 'utf-8'
            soupb = BeautifulSoup(res.text, 'html.parser')
            info = soupb.select('.show-info')[0].text
            e = soupb.select('.show-content')[0].text
    
            print('标题:' + t)
            print('链接:' + newsUrl)
            print('描述:' + d)
            print('正文:' + e)
            getNews(newsUrl)
            break
     
    

  • 相关阅读:
    Delphi文件操作读文件写文件操作文件
    delphi7 开发ActiveX的学习备忘录
    delphi延时函数详细说明
    delphi如何保存和读取utf8的文本文件
    Delphi中线程的释放介绍[转]
    python IsWindowEnabled遍历windows的所有窗口并输出窗口标题
    如何把 XML 文件显示为 HTML 表格
    delphi把Frame嵌入一个Form
    Delphi如何实现内存共享
    about linux vps
  • 原文地址:https://www.cnblogs.com/170he/p/8763050.html
Copyright © 2011-2022 走看看