1. 引言
在當今的學術研究和大數據分析領域,高效獲取和分析學術文獻數據具有重要意義。中國知網(CNKI)作為國內最權威的學術資源平臺之一,包含了海量的期刊論文、會議論文和學位論文。然而,手動收集和分析這些數據不僅耗時耗力,而且難以進行大規模的趨勢分析。
本文將介紹如何使用Python實現知網文獻的自動化爬取、存儲與可視化,涵蓋以下關鍵技術點:
- 爬蟲技術:使用
**<font style="color:rgb(64, 64, 64);background-color:rgb(236, 236, 236);">requests</font>**
和**<font style="color:rgb(64, 64, 64);background-color:rgb(236, 236, 236);">BeautifulSoup</font>**
抓取知網數據 - 反爬策略:模擬瀏覽器行為,處理驗證碼
- 數據存儲:使用
**<font style="color:rgb(64, 64, 64);background-color:rgb(236, 236, 236);">MongoDB</font>**
或**<font style="color:rgb(64, 64, 64);background-color:rgb(236, 236, 236);">MySQL</font>**
存儲結構化數據 - 數據分析與可視化:使用
**<font style="color:rgb(64, 64, 64);background-color:rgb(236, 236, 236);">Pandas</font>**
進行數據處理,**<font style="color:rgb(64, 64, 64);background-color:rgb(236, 236, 236);">Pyecharts</font>**
生成可視化圖表
2. 技術方案設計
2.1 整體架構
1. 數據采集層:Python爬蟲(requests + BeautifulSoup)
2. 數據存儲層:MongoDB/MySQL
3. 數據分析層:Pandas數據清洗
4. 可視化層:Pyecharts/Matplotlib
2.2 技術選型
技術 | 用途 |
---|---|
**<font style="color:rgb(64, 64, 64);background-color:rgb(236, 236, 236);">requests</font>** | 發送HTTP請求 |
**<font style="color:rgb(64, 64, 64);background-color:rgb(236, 236, 236);">BeautifulSoup</font>** | HTML解析 |
**<font style="color:rgb(64, 64, 64);background-color:rgb(236, 236, 236);">Selenium</font>** | 處理動態頁面(如驗證碼) |
**<font style="color:rgb(64, 64, 64);background-color:rgb(236, 236, 236);">Pandas</font>** | 數據清洗與分析 |
**<font style="color:rgb(64, 64, 64);background-color:rgb(236, 236, 236);">Pyecharts</font>** | 交互式可視化 |
**<font style="color:rgb(64, 64, 64);background-color:rgb(236, 236, 236);">MongoDB</font>** | 非關系型數據庫存儲 |
3. 爬蟲實現
3.1 環境準備
3.2 爬取知網搜索頁
import requests
from bs4 import BeautifulSoup
import pandas as pdheaders = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"
}def crawl_cnki(keyword, page=1):url = f"https://www.cnki.net/search/result?searchKey={keyword}&page={page}"response = requests.get(url, headers=headers)soup = BeautifulSoup(response.text, 'html.parser')papers = []for item in soup.select(".result-item"):paper = {"title": item.select_one(".title").get_text(strip=True),"author": item.select_one(".author").get_text(strip=True),"institution": item.select_one(".institution").get_text(strip=True),"date": item.select_one(".date").get_text(strip=True),"citations": int(item.select_one(".citations").get_text(strip=True))}papers.append(paper)return papers# 示例:爬取"人工智能"相關論文(前3頁)
all_papers = []
for page in range(1, 4):all_papers.extend(crawl_cnki("人工智能", page))
3.3 處理反爬機制
from selenium import webdriver
from selenium.webdriver.common.by import Bydef crawl_with_selenium(keyword):driver = webdriver.Chrome()driver.get(f"https://www.cnki.net/search/result?searchKey={keyword}")# 處理可能的驗證碼try:captcha = driver.find_element(By.ID, "captcha")if captcha:input("請手動完成驗證碼后按回車繼續...")except:pass# 獲取渲染后的頁面源碼soup = BeautifulSoup(driver.page_source, 'html.parser')driver.quit()return parse_results(soup) # 復用之前的解析函數
4. 數據存儲
4.1 MongoDB存儲
from pymongo import MongoClientclient = MongoClient("mongodb://localhost:27017/")
db = client["cnki_research"]
collection = db["papers"]# 批量插入數據
collection.insert_many(all_papers)
4.2 MySQL存儲(替代方案)
import mysql.connectorconn = mysql.connector.connect(host="localhost",user="root",password="123456",database="cnki_db"
)cursor = conn.cursor()
cursor.execute("""CREATE TABLE IF NOT EXISTS papers (id INT AUTO_INCREMENT PRIMARY KEY,title VARCHAR(255),author VARCHAR(100),institution VARCHAR(255),publish_date DATE,citations INT)
""")# 插入數據
for paper in all_papers:cursor.execute("""INSERT INTO papers (title, author, institution, publish_date, citations)VALUES (%s, %s, %s, %s, %s)""", (paper["title"], paper["author"], paper["institution"], paper["date"], paper["citations"]))conn.commit()
5. 數據分析與可視化
5.1 數據清洗
df = pd.DataFrame(all_papers)
df["date"] = pd.to_datetime(df["date"]) # 轉換日期格式
df["year"] = df["date"].dt.year # 提取年份# 按年份統計論文數量
year_counts = df["year"].value_counts().sort_index()
5.2 Pyecharts可視化
(1) 年度發文趨勢(折線圖)
from pyecharts.charts import Lineline = (Line().add_xaxis(year_counts.index.tolist()).add_yaxis("發文量", year_counts.values.tolist()).set_global_opts(title_opts={"text": "人工智能領域年度發文趨勢"},toolbox_opts={"feature": {"saveAsImage": {}}})
)
line.render("annual_trend.html")
(2) 機構發文排名(柱狀圖)
from pyecharts.charts import Bartop_institutions = df["institution"].value_counts().head(10)bar = (Bar().add_xaxis(top_institutions.index.tolist()).add_yaxis("發文量", top_institutions.values.tolist()).set_global_opts(title_opts={"text": "Top 10研究機構"},xaxis_opts={"axis_label": {"rotate": 45}})
)
bar.render("institutions_ranking.html")
(3) 關鍵詞共現分析(需先提取關鍵詞)
from pyecharts.charts import WordCloud# 假設有關鍵詞數據
keywords = {"機器學習": 120,"深度學習": 95,"自然語言處理": 78,"計算機視覺": 65
}wordcloud = (WordCloud().add("", list(keywords.items()), word_size_range=[20, 100]).set_global_opts(title_opts={"text": "研究熱點關鍵詞"})
)
wordcloud.render("keywords.html")
6. 結論
本文實現了:
- 基于Python的知網文獻自動化爬取
- 多存儲方案(MongoDB/MySQL)
- 交互式可視化分析
該方法可應用于:
- 學術趨勢研究
- 學科熱點分析
- 機構科研能力評估