zoukankan      html  css  js  c++  java
  • 获取一篇新闻的全部信息

    给定一篇新闻的链接newsUrl,获取该新闻的全部信息

    标题、作者、发布单位、审核、来源

    发布时间:转换成datetime类型

    点击:

    • newsUrl
    • newsId(使用正则表达式re)
    • clickUrl(str.format(newsId))
    • requests.get(clickUrl)
    • newClick(用字符串处理,或正则表达式)
    • int()
    # -*- coding: utf-8 -*-
    import re
    import requests
    from bs4 import BeautifulSoup
    from datetime import datetime
    def click(url):
        clickurl='http://oa.gzcc.cn/api.php?op=count&id=&modelid=80'
        res=requests.get(clickurl)
        click=res.text.split('.html')[-1].lstrip("('").rstrip("');")
        return click
    def newsnum(url):
        newsid=re.match('http://news.gzcc.cn/html/2019/xiaoyuanxinwen_0320/(.*).html',url).group(1)
        return newsid
    def newstime(url,soup):
        '''
        time=soup.select('.show-info')[0].text[5:24]
        time=soup.select('.show-info')[0].text.split()[0].lstrip('发布的时间为')
        time=soup.select('.show-info')[0].text.split()[0].lstrip('发布的内容为')
    ''' newsdate=soup.select('.show-info')[0].text.split()[0].split(':')[1] newstime=soup.select('.show-info')[0].text.split()[1] time=newsdate+' '+newstime time=datetime.strptime(time,'%Y-%m-%d %H:%M:%S') return time def news(url): res=requests.get(url) res.encoding='utf-8' soup=BeautifulSoup(res.text,'html.parser') title=soup.select('.show-title')[0].text author=soup.select('.show-info')[0].text.split()[2] auditor=soup.select('.show-info')[0].text.split()[3] comefrom=soup.select('.show-info')[0].text.split()[4] detail=soup.select('.show-content p')[0].text newsid=newsnum(url) time=newstime(url,soup) clicktime=click(url) p=print(newsid,title,time,author,auditor,comefrom,detail,clicktime) return p url="http://news.gzcc.cn/html/2019/xiaoyuanxinwen_0320/11029.html" news(url)
    运行结果:



  • 相关阅读:
    DP(动态规划)学习心得
    hloj#402 护卫队解题讨论
    hloj#168“倒牛奶”解题讨论
    贪心:畜栏预定
    区间问题
    离散化
    差分
    浏览器的工作原理幕后揭秘的部分笔迹摘要
    python之阶乘的小例子
    关于python中urllib.urlencode的时候出错:UnicodeEncodeError: ‘ascii’的记录
  • 原文地址:https://www.cnblogs.com/068zhengda/p/10651371.html
Copyright © 2011-2022 走看看