爬取校园新闻首页的新闻的详情,使用正则表达式,函数抽离
生活随笔
收集整理的這篇文章主要介紹了
爬取校园新闻首页的新闻的详情,使用正则表达式,函数抽离
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
import requests
import reurl = "http://news.gzcc.cn/html/xiaoyuanxinwen/"
res = requests.get(url)
res.encoding = 'utf-8'# 利用BeautifulSoup的HTML解析器,生成結構樹
from bs4 import BeautifulSoupsoup = BeautifulSoup(res.text, 'html.parser')def getClickCount(url):HitUrl = 'http://oa.gzcc.cn/api.php?op=count&id=9183&modelid=80'hitNumber = requests.get(HitUrl).text.split('.html')[-1].lstrip("('").rstrip("');")print("點擊次數:", hitNumber)re.match('http://news.gzcc.cn/html/2018/xiaoyuanxinwen(.*).html', url).group(1).split('/')[1]print('新聞編號:', re.search('\_(.*).html', url).group(1))def getNewDetail(url):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# 首頁文章描述description = news.select('.news-list-description')[0].text# 首頁文章信息info = news.select('.news-list-info')[0].text# 首頁文章鏈接href = news.select('a')[0]['href']url = hrefres = requests.get(url)res.encoding = 'utf-8'soup = BeautifulSoup(res.text, 'html.parser')# 獲取每篇文章的信息newinfo = soup.select('.show-info')[0].text# 獲取文章內容content = soup.select('#content')[0].text# 日期date = newinfo.split()[0]# 當日時間time = newinfo.split()[1]# 作者author = newinfo.split()[2]# 審核checker = newinfo.split()[3]# 來源source = newinfo.split()[4]# 攝影Photography = newinfo.split()[5]print('------------------------------------------------------------------------------')print("文章標題:" + title)print("\n文章描述:" + description)print("\n文章信息:\n" + date + ' ' + time + '\n' + author + '\n' + checker + '\n' + source+ '\n' + Photography)getClickCount(href)#點擊次數、新聞編號print("\n文章鏈接:" + href)print(content)print('------------------------------------------------------------------------------')getNewDetail(url)
轉載于:https://www.cnblogs.com/FZW1874402927/p/8747466.html
總結
以上是生活随笔為你收集整理的爬取校园新闻首页的新闻的详情,使用正则表达式,函数抽离的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 多线程技术点二
- 下一篇: linux nc 详解