實時數據挖掘
目錄
序言
通過使用Selenium庫實現對新浪財經股票實時數據、東方財富網、裁判文書網、巨潮資訊網的實時數據挖掘。由於這部分內容涉及爬蟲進階知識,所以我把這部分內容歸於爬蟲專欄。
1. 新浪財經實時數據挖掘實戰
1.1 獲取網頁源代碼
使用無界面瀏覽器方式獲取源代碼
from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
browser = webdriver.Chrome(executable_path=r'C:\Users\wwww\AppData\Local\Google\Chrome\Application\chromedriver.exe',
options=chrome_options)
browser.get("http://finance.sina.com.cn/realstock/company/sh000001/nc.shtml")
data = browser.page_source
browser.quit()
print(data)
部分結果如圖所示:
1.2 數據提取
我們要提取上證綜合指數,因爲這個指數是唯一的且不斷變化,首先我們要定位網頁源代碼。
可以得到,如果上證綜合指數是下降的話,class爲down;同理,如果上證綜合指數是上升的話,class爲up。
from selenium import webdriver
import re
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
browser = webdriver.Chrome(executable_path=r'C:\Users\wwww\AppData\Local\Google\Chrome\Application\chromedriver.exe',
options=chrome_options)
browser.get('http://finance.sina.com.cn/realstock/company/sh000001/nc.shtml')
data = browser.page_source
browser.quit()
p_price = '<div id="price" class=".*?">(.*?)</div>'
price = re.findall(p_price, data)
print(price) # 上證綜合指數的股價
結果: [‘2976.53’]
2. 東方財富網數據挖掘實戰
2.1 獲取網頁源代碼
首先我們進入東方財富網,然後點擊搜索阿里巴巴,我們爬取阿里巴巴的源代碼。
我們使用無界面瀏覽器模式來獲取該網站源代碼:
from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
browser = webdriver.Chrome(executable_path=r'C:\Users\wwww\AppData\Local\Google\Chrome\Application\chromedriver.exe',
options=chrome_options)
browser.get('http://so.eastmoney.com/news/s?keyword=阿里巴巴')
data = browser.page_source
print(data)
browser.quit()
部分結果如下圖所示:
2.2 編寫正則表達式提取數據
我們的要提取新聞題目、鏈接、日期,首先查看網站源代碼:
有了源代碼,我們就可以編寫正則表達式來提取這一部分的數據了。
p_title = '<div class="news-item"><h3><a href=".*?">(.*?)</a>'
p_href = '<div class="news-item"><h3><a href="(.*?)">.*?</a>'
p_date = '<p class="news-desc">(.*?)</p>'
title = re.findall(p_title, data)
href = re.findall(p_href, data)
date = re.findall(p_date, data, re.S)
2.3 數據的清洗及打印輸出
提取的數據往往不美觀並且不易使用,所以我們必須要對數據進行清洗,比如說日期裏有一些不需要的新聞摘要等內容,它們中間通過空格分割,所以我們可以用split()函數進行分割。
代碼如下:
for i in range(len(title)):
title[i] = re.sub('<.*?>', '', title[i])
date[i] = date[i].split(' ')[0]
print(str(i+1) + '.' + title[i] + ' - ' + date[i])
print(href[i])
2.4 實戰代碼
將之前的代碼做一個彙總並加以改進,就可以實現對東方財經網數據的挖掘了!
from selenium import webdriver
import re
def dongfang(company):
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
browser = webdriver.Chrome(executable_path=r'C:\Users\wwww\AppData\Local\Google\Chrome\Application\chromedriver.exe',
options=chrome_options)
url = 'http://so.eastmoney.com/news/s?keyword=' + company
browser.get(url)
data = browser.page_source
browser.quit()
p_title = '<div class="news-item"><h3><a href=".*?">(.*?)</a>'
p_href = '<div class="news-item"><h3><a href="(.*?)">.*?</a>'
p_date = '<p class="news-desc">(.*?)</p>'
title = re.findall(p_title, data)
href = re.findall(p_href, data)
date = re.findall(p_date, data, re.S)
for i in range(len(title)):
title[i] = re.sub('<.*?>', '', title[i])
date[i] = date[i].split(' ')[0]
print(str(i+1) + '.' + title[i] + ' - ' + date[i])
print(href[i])
companys = ['華能信託', '阿里巴巴', '騰訊', '京東']
for i in companys:
try:
dongfang(i)
print(i + '該公司東方財富網爬取成功')
except:
print(i + '該公司東方財富網爬取失敗')
代碼執行結果如下:
3. 裁判文書網數據挖掘實戰
裁判文書網是最權威的生效裁判文書公示網站,對於金融行業內部風控和輿情監控有較高的價值,其首頁如圖所示:
要獲取該網站的內容,我們以輸入當事人爲例,使用Selenium庫以及XPath動態爬取該網站數據:
from selenium import webdriver
import time
browser = webdriver.Chrome(executable_path=r'C:\Users\wwww\AppData\Local\Google\Chrome\Application\chromedriver.exe')
browser.get('http://wenshu.court.gov.cn/')
browser.maximize_window()
browser.find_element_by_xpath('//*[@id="_view_1540966814000"]/div/div[1]/div[2]/input').clear()
browser.find_element_by_xpath('//*[@id="_view_1540966814000"]/div/div[1]/div[2]/input').send_keys('當事人')
browser.find_element_by_xpath('//*[@id="_view_1540966814000"]/div/div[1]/div[3]').click()
time.sleep(10)
data = browser.page_source
browser.quit()
print(data)
4. 巨潮資訊網數據挖掘實戰
巨潮資訊網是中國證券監督管理委員會指定的上市公司信息披露網站,創建於1995年,是國內最早的證券信息專業網站,也是國內首家全面披露滬3000多家上市公司公告信息和市場數據的大型證券專業網站。同樣,我們還是挖掘標題、鏈接、日期信息。
4.1 獲取網頁源代碼
from selenium import webdriver
browser = webdriver.Chrome(executable_path=r'C:\Users\wwww\AppData\Local\Google\Chrome\Application\chromedriver.exe')
url = 'http://www.cninfo.com.cn/new/fulltextSearch?notautosubmit=&keyWord=理財'
browser.get(url)
data = browser.page_source
print(data)
4.2 編寫正則表達式提取數據
p_title = '<span title="" class="r-title">(.*?)</span>'
p_href = '<a target="_blank" href="(.*?)data-id='
p_date = '<span class="time">(.*?)</span>'
title = re.findall(p_title, data)
href = re.findall(p_href, data)
date = re.findall(p_date, data, re.S)
4.3 數據清洗及打印輸出
for i in range(len(title)):
title[i] = re.sub(r'<.*?>', '', title[i])
href[i] = 'http://www.cninfo.com.cn' + href[i]
href[i] = re.sub('amp;', '', href[i])
date[i] = date[i].split(' ')[0]
print(str(i+1) + '.' + title[i] + ' - ' + date[i])
print(href[i])
4.4 實戰代碼
import re
from selenium import webdriver
def juchao(keyword):
browser = webdriver.Chrome(executable_path=r'C:\Users\wwww\AppData\Local\Google\Chrome\Application\chromedriver.exe')
url = 'http://www.cninfo.com.cn/new/fulltextSearch?notautosubmit=&keyWord=' + keyword
browser.get(url)
data = browser.page_source
browser.quit()
p_title = '<span title="" class="r-title">(.*?)</span>'
p_href = '<a target="_blank" href="(.*?)data-id='
p_date = '<span class="time">(.*?)</span>'
title = re.findall(p_title, data)
href = re.findall(p_href, data)
date = re.findall(p_date, data, re.S)
for i in range(len(title)):
title[i] = re.sub(r'<.*?>', '', title[i])
href[i] = 'http://www.cninfo.com.cn' + href[i]
href[i] = re.sub('amp;', '', href[i])
date[i] = date[i].split(' ')[0]
print(str(i+1) + '.' + title[i] + ' - ' + date[i])
print(href[i])
file1 = open('D:\\python代碼\\金融大數據挖掘與分析\\Chapter9\\數據挖掘報告.txt', 'a')
file1.write(keyword + '數據挖掘completed!' + '\n' + '\n')
for i in range(len(title)):
file1.write(str(i+1) + '.' + title[i] + ' - ' + date[i] + '\n')
file1.write(href[i] + '\n') # '\n'表示換行
file1.write('——————————————————————————————' + '\n' + '\n')
file1.close()
keywords = ['理財', '現金管理', '紓困']
for i in keywords:
juchao(i)
部分結果:
結尾
通過對一些網站的信息爬取,掌握了一些爬取方法以及爬蟲技巧,還能熟練使用Python語言編寫代碼,從而有利於人們對這些數據的分析以及利用,從而爲社會作出更好的決策。