爬取基礎2
阿新 • • 發佈:2018-04-11
pre ptime detail odi etime 發布 int image 時間格式
1.取出一個新聞列表頁的全部新聞 包裝成函數。
2.獲取總的新聞篇數,算出新聞總頁數。
3.獲取全部新聞列表頁的全部新聞詳情。
import requests from bs4 import BeautifulSoup from datetime import datetime import re # 獲取新聞點擊次數 def getNewsId(url): newsId = re.findall(r‘\_(.*).html‘, url)[0][-4:] clickUrl = ‘http://oa.gzcc.cn/api.php?op=count&id={}&modelid=80‘.format(newsId) clickRes = requests.get(clickUrl) # 利用正則表達式獲取新聞點擊次數 clickCount = int(re.search("hits‘\).html\(‘(.*)‘\);", clickRes.text).group(1)) return clickCount # 獲取新聞細節 def getNewsDetail(newsUrl): resd = requests.get(newsUrl) resd.encoding = ‘utf-8‘ soupd = BeautifulSoup(resd.text, ‘html.parser‘) content = soupd.select(‘#content‘)[0].text info = soupd.select(‘.show-info‘)[0].text # 調用getNewsId()獲取點擊次數 count = getNewsId(newsUrl) # 識別時間格式 date = re.search(‘(\d{4}.\d{2}.\d{2}\s\d{2}.\d{2}.\d{2})‘, info).group(1) # 識別一個至三個數據 if(info.find(‘作者:‘)>0): author = re.search(‘作者:((.{2,4}\s|.{2,4}、){1,3})‘, info).group(1) if(info.find(‘審核:‘)>0): check = re.search(‘審核:((.{2,4}\s){1,3})‘, info).group(1) if(info.find(‘來源:‘)>0): sources = re.search(‘來源:(.*)\s*攝|點‘, info).group(1) # 用datetime將時間字符串轉換為datetime類型 dateTime = datetime.strptime(date, ‘%Y-%m-%d %H:%M:%S‘) # 利用format對字符串進行操作 print(‘發布時間:{0}\n作者:{1}\n審核:{2}\n來源:{3}\n點擊次數:{4}‘.format(dateTime, author, check, sources, count)) print(content) # 獲取列表頁新聞 def getListPage(listUrl): res = requests.get(listUrl) res.encoding = ‘utf-8‘ soup = BeautifulSoup(res.text, ‘html.parser‘) for new in soup.select(‘li‘): if len(new.select(‘.news-list-title‘)) > 0: title = new.select(‘.news-list-title‘)[0].text description = new.select(‘.news-list-description‘)[0].text newsUrl = new.select(‘a‘)[0][‘href‘] print(‘標題:{0}\n內容:{1}\n鏈接:{2}‘.format(title, description, newsUrl)) # 調用getNewsDetail()獲取新聞詳情 getNewsDetail(newsUrl) break # 獲取新聞總頁數 def getListTotalNumber(firstUrl): res = requests.get(firstUrl) res.encoding = ‘utf-8‘ soup = BeautifulSoup(res.text, ‘html.parser‘) listCount = int(soup.select(‘.a1‘)[0].text.rstrip(‘條‘)) // 10 + 1 return listCount firstUrl = ‘http://news.gzcc.cn/html/xiaoyuanxinwen/‘ getListPage(firstUrl) # 獲取新聞總頁數 n=getListTotalNumber(firstUrl) for i in range(2,n): listUrl= ‘http://news.gzcc.cn/html/xiaoyuanxinwen/{}.html‘.format(i) getListPage(listUrl)
4.找一個自己感興趣的主題,進行數據爬取,並進行分詞分析。不能與其它同學雷同。
import requests from bs4 import BeautifulSoup # 獲取新聞細節 def getNewsDetail(newsUrl): resd = requests.get(newsUrl) resd.encoding = ‘utf-8‘ soupd = BeautifulSoup(resd.text, ‘html.parser‘) content = soupd.select(‘.cont‘)[0].text.rsplit("復仇者聯盟2:奧創紀元下載地址:http://www.80smp4.net/mp4_3gp/26733/")[0] print(‘內容:{}‘.format(content)) # 獲取列表頁新聞 def getListPage(listUrl): res = requests.get(listUrl) res.encoding = ‘utf-8‘ soup = BeautifulSoup(res.text, ‘html.parser‘) for new in soup.select(‘div‘): if len(new.select(‘.ph_u‘)) > 0: description = new.select(‘.title‘)[0].text.split()[0] newsUrl = new.select(‘a‘)[0][‘href‘] # print(‘標題:{0}\n內容:{1}\n鏈接:{2}‘.format(title, description, newsUrl)) # 調用getNewsDetail()獲取新聞詳情 print(‘片名:{0}\n連接:{1}‘.format(description,newsUrl)) getNewsDetail(newsUrl) break firstUrl = ‘http://www.80smp4.net/movie/‘ getListPage(firstUrl)
爬取基礎2