zoukankan      html  css  js  c++  java
  • 使用正则表达式,取得点击次数,函数抽离

    import requests
    from  bs4 import  BeautifulSoup
    from datetime import datetime
    import locale
    import re
    locale.setlocale(locale.LC_CTYPE,'chinese')
    newsurl = 'http://news.gzcc.cn/html/xiaoyuanxinwen/'
    res = requests.get(newsurl) 
    res.encoding = 'utf-8'
    soup = BeautifulSoup(res.text,'html.parser')
    
    def getClickCount(newsUrl): 
        newsId = re.findall('\_(.*).html', newsUrl)[0].split('/')[1] 
        clickUrl = 'http://oa.gzcc.cn/api.php?op=count&id=9183&modelid=80'.format(newsId)
        clickStr = requests.get(clickUrl).text
        return(re.search("hits').html('(.*)');",clickStr).group(1))
    
    def getNewDetail(newsUrl):  
        resd = requests.get(newsUrl)  
        resd.encoding = 'utf-8'
        soupd = BeautifulSoup(resd.text, 'html.parser')
        print('标题:' + title)
        print('描述:' + description)
        print('链接:' + newsUrl)
        info = soupd.select('.show-info')[0].text
        time = re.search('发布时间:(.*) xa0xa0 xa0xa0作者:', info).group(1)
        dtime = datetime.strptime(time, '%Y-%m-%d %H:%M:%S')  
        print('发布时间:{}'.format(dtime))
        print('作者:' + re.search('作者:(.*)审核:', info).group(1))
        print('审核:' + re.search('审核:(.*)来源:', info).group(1))
        print('来源:' + re.search('来源:(.*)摄影:', info).group(1))
        print('摄影:' + re.search('摄影:(.*)点击', info).group(1))
        print('点击次数:' + getClickCount(a))
        print('正文:'+soupd.select('.show-content')[0].text)
    
    for news in soup.select('li'):
        if len(news.select('.news-list-title'))>0:
            title = news.select('.news-list-title')[0].text
            description = news.select('.news-list-description')[0].text
            a = news.a.attrs['href']
            getNewDetail(a)
            break
  • 相关阅读:
    CentOS 6.3 从0开始配置防火墙规则
    ppt删除所有墨迹注释
    生日、毕业与大学的回忆
    20171201-构建之法:现代软件工程-阅读笔记
    软件工程课程总结
    课后作业-阅读任务-阅读提问-4
    Forward团队-爬虫豆瓣top250项目-项目总结
    Forward团队-爬虫豆瓣top250项目-最终程序
    课后作业-阅读任务-阅读提问-3
    20171117-构建之法:现代软件工程-阅读笔记
  • 原文地址:https://www.cnblogs.com/129lai/p/8758402.html
Copyright © 2011-2022 走看看