作業來源:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE1/homework/3002
0.從新聞url獲取點擊次數,並整理成函數
- newsUrl
- newsId(re.search())
- clickUrl(str.format())
- requests.get(clickUrl)
- re.search()/.split()
- str.lstrip(),str.rstrip()
- int
- 整理成函數
- 獲取新聞發布時間及類型轉換也整理成函數
import re url='http://news.gzcc.cn/html/2019/xiaoyuanxinwen_0320/11029.html' clickurl='http://oa.gzcc.cn/api.php?op=count&id=11029&modelid=80'
re.match('http://news.gzcc.cn/html/2019/xiaoyuanxinwen_0320/(.*).html',url)
re.match('http://news.gzcc.cn/html/2019/xiaoyuanxinwen_0320/(.*).html',url).groups(0)
re.search('/(\d*).html',url).groups(1)
re.findall('(\d+)',url)
結果如下:
1.從新聞url獲取新聞詳情: 字典,anews
import requests from bs4 import BeautifulSoup from datetime import datetime import re def click(url): id=re.findall('(\d{1,5})',url)[-1] clickUrl = 'http://oa.gzcc.cn/api.php?op=count&id=11029&modelid=80'.format(id) resClick = requests.get(clickUrl) newsClick = int(resClick.text.split('.html')[-1].lstrip("('").rstrip("');")) return newsClick def newsdt(showinfo): newsDate = showinfo.split()[0].split(':')[1] newsTime = showinfo.split()[1] newsDT = newsDate+' '+newsTime dt = datetime.strptime(newsDT, '%Y-%m-%d %H:%M:%S') return dt def anews(url): newsDetail = {} res = requests.get(url) res.encoding ='utf-8' soup = BeautifulSoup(res.text,'html.parser') newsDetail['nenewsTitle'] =soup.select('.show-title')[0].text showinfo = soup.select('.show-info')[0].text newsDetail['newsDT']=newsdt(showinfo) newsDetail['newsClick'] =click(newsUrl) return newsDetail newsUrl='http://news.gzcc.cn/html/2005/xiaoyuanxinwen_0710/4.html' anews(newsUrl)
結果:
2.從列表頁的url獲取新聞url:列表append(字典) alist
- 獲取列表數據
listurl = 'http://news.gzcc.cn/html/xiaoyuanxinwen/' res = requests.get(listurl) res.encoding ='utf-8' soupn = BeautifulSoup(res.text,'html.parser') # a=soupn.select('a') soupn
2.過濾過濾數據,只獲取列表的新聞信息
for news in soupn.select('li'): if news.select('.news-list-title'): print(news) newsUrl = news.a['href'] print(news.a['href'])
3.獲取整頁信息
def alist(listUrl): res = requests.get(listurl) res.encoding ='utf-8' soup = BeautifulSoup(res.text,'html.parser') newsList =[] for news in soupn.select('li'): if len(news.select('.news-list-title'))>0: newsUrl = news.select('a')[0]['href'] newsDesc = news.select('.news-list-description')[0].text newsDict = anews(newsUrl) newsDict['newsUrl'] = newsUrl newsDict['description'] = newsDesc newsList.append(newsDict) return newsList listUrl = 'http://news.gzcc.cn/html/xiaoyuanxinwen/' alist(listUrl)
3.生成所頁列表頁的url並獲取全部新聞 :列表extend(列表) allnews
*每個同學爬學號尾數開始的10個列表頁
- 獲取多頁信息
- 截取以學號尾數開始的10個列表頁
listUrl = 'http://news.gzcc.cn/html/xiaoyuanxinwen/' allnews = alist(listUrl) for i in range(7,17): #學號為7,截取10頁 listUrl = 'http://news.gzcc.cn/html/xiaoyuanxinwen/{}.html'.format(i) allnews.extend(alist(listUrl)) len(allnews)
4.設置合理的爬取間隔
import time
import random
time.sleep(random.random()*3)
import time import random listUrl = 'http://news.gzcc.cn/html/xiaoyuanxinwen/' allnews = alist(listUrl) for i in range(1,170): #學號為7,截取10頁 listUrl = 'http://news.gzcc.cn/html/xiaoyuanxinwen/{}.html'.format(i) allnews.extend(alist(listUrl)) time.sleep(random.random()*3) #設置每3秒爬取一次
print(alist(listUrl)) len(allnews)
5.用pandas做簡單的數據處理並保存
保存到csv或excel文件
newsdf.to_csv(r'F:\duym\爬蟲\gzccnews.csv')
- 使用pandas函數整理爬取的數據
- 列表的形式打印數據
- 顯示 “newsClick” 游覽次數大於2337的新聞
- 生成csv文件
newsdf.to_csv(r'E:\gzcc.csv')