前兩篇我利用爬蟲進行營銷推廣,并且寫了一個品牌口碑爬蟲的代碼示例。現在根據轉向行業發展趨勢,可能是希望收集數據來分析市場動向、競爭對手動態或者新興技術趨勢。
技術實現方面,需要選擇合適的工具和庫。Python的requests和BeautifulSoup是常見組合,但如果目標網站有動態加載內容,可能需要使用Selenium或Scrapy-Splash。此外,數據存儲和分析部分可能需要使用Pandas進行數據處理,以及NLP庫進行關鍵詞提取和趨勢分析。
以下是我寫的另一個合法合規的爬蟲代碼示例,用于抓取公開的行業發展趨勢數據(如行業新聞、政策文件、市場報告摘要等)。本示例以抓取行業新聞網站的標題和摘要為例,僅用于學習參考,需遵守目標網站的robots.txt
協議并控制爬取頻率。
目標:爬取行業新聞標題、摘要、發布時間,分析高頻關鍵詞和趨勢變化。
代碼實現(Python)
import requests
from bs4 import BeautifulSoup
import time
import pandas as pd
from collections import Counter
import jieba # 中文分詞庫# 配置參數(需根據目標網站結構調整)
BASE_URL = "https://36kr.com/hot-list/catalog" # 示例網站,實際需替換為合法目標
HEADERS = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36","Referer": "https://36kr.com/"
}
MAX_PAGES = 3 # 控制爬取頁數
DELAY = 3 # 請求間隔(秒)def crawl_industry_news():news_data = []for page in range(1, MAX_PAGES + 1):url = f"{BASE_URL}/page/{page}"try:response = requests.get(url, headers=HEADERS, timeout=10)response.raise_for_status()soup = BeautifulSoup(response.text, 'html.parser')# 定位新聞條目(根據實際頁面結構調整選擇器)articles = soup.find_all('div', class_='article-item')for article in articles:title = article.find('a', class_='title').text.strip()summary = article.find('div', class_='summary').text.strip()publish_time = article.find('span', class_='time').text.strip()link = article.find('a', class_='title')['href']news_data.append({"title": title,"summary": summary,"time": publish_time,"link": link})print(f"第 {page} 頁爬取完成")time.sleep(DELAY) # 控制頻率except Exception as e:print(f"爬取失敗: {e}")break# 保存為CSVdf = pd.DataFrame(news_data)df.to_csv("industry_news.csv", index=False, encoding='utf-8-sig')return dfdef analyze_trends(df):# 合并所有文本內容all_text = ' '.join(df['title'] + ' ' + df['summary'])# 中文分詞與停用詞過濾words = jieba.lcut(all_text)stopwords = set(['的', '是', '在', '和', '了', '等', '與', '為']) # 自定義停用詞表filtered_words = [word for word in words if len(word) > 1 and word not in stopwords]# 統計高頻詞word_counts = Counter(filtered_words)top_words = word_counts.most_common(20)print("行業高頻關鍵詞Top20:")for word, count in top_words:print(f"{word}: {count}次")if __name__ == '__main__':df = crawl_industry_news()analyze_trends(df)
關鍵功能說明
- 數據抓取:
- 爬取新聞標題、摘要、發布時間和鏈接。
- 通過
time.sleep(DELAY)
控制請求頻率,避免觸發反爬。
- 數據分析:
- 使用
jieba
進行中文分詞,統計高頻關鍵詞。 - 輸出Top20行業關鍵詞,輔助判斷趨勢方向(如“AI”、“碳中和”)。
- 使用
擴展場景與數據源
1. 政策文件抓取(示例:中國政府網)
# 抓取政策文件標題和發布日期
def crawl_government_policies():url = "http://www.gov.cn/zhengce/zhengceku/"response = requests.get(url, headers=HEADERS)soup = BeautifulSoup(response.text, 'html.parser')policies = []for item in soup.select('.news_box .list li'):title = item.find('a').text.strip()date = item.find('span').text.strip()policies.append({"title": title, "date": date})return pd.DataFrame(policies)
2. 專利趨勢分析(示例:中國專利數據庫)
# 需使用Selenium模擬瀏覽器(動態加載頁面)
from selenium import webdriverdef crawl_patents(keyword="人工智能"):driver = webdriver.Chrome()driver.get("http://pss-system.cnipa.gov.cn/")driver.find_element_by_id("searchKey").send_keys(keyword)driver.find_element_by_id("searchBtn").click()time.sleep(5) # 等待加載patents = []# 解析專利名稱、申請號、申請人等數據# (需根據實際頁面結構編寫解析邏輯)driver.quit()return patents
3. 招聘趨勢分析(示例:拉勾網)
# 需處理反爬機制(如加密參數)
def crawl_job_trends(keyword="數據分析"):url = "https://www.lagou.com/jobs/list_數據分析"headers = {..., "Cookie": "需自行獲取有效Cookie"}response = requests.get(url, headers=headers)# 解析職位數量、薪資范圍、技能要求等
合法性與風險規避
- 合規原則:
- 僅抓取公開數據,避開需登錄的頁面。
- 遵守目標網站
robots.txt
。
- 反爬應對:
- 使用代理IP池(如
requests
+proxy
)。 - 動態User-Agent輪換(庫:
fake_useragent
)。
- 使用代理IP池(如
- 數據脫敏:
- 不存儲無關的個人信息(如姓名、電話)。
數據分析與可視化(擴展)
-
時間趨勢圖:
import matplotlib.pyplot as plt # 按月份統計新聞數量 df['month'] = pd.to_datetime(df['time']).dt.to_period('M') monthly_counts = df.groupby('month').size() monthly_counts.plot(kind='line', title='行業新聞月度趨勢') plt.show()
-
詞云生成:
from wordcloud import WordCloud text = ' '.join(filtered_words) wordcloud = WordCloud(font_path='SimHei.ttf').generate(text) plt.imshow(wordcloud) plt.axis('off') plt.show()
總結
通過合法爬蟲抓取行業新聞、政策、專利等公開數據,結合自然語言處理(NLP)和時間序列分析,可快速識別行業趨勢。關鍵點:
- 聚焦公開數據,避免法律風險。
- 動態應對反爬(頻率控制、代理IP)。
- 數據驅動決策:將爬取結果轉化為可視化報告或關鍵詞洞察。
以上就是我寫的全部內容,具體情況還得參考實際做調整,但是大體框架是沒錯的。