一文教你Python如何快速精準(zhǔn)抓取網(wǎng)頁數(shù)據(jù)
本文將使用requests和BeautifulSoup這兩個流行的庫來實現(xiàn)。
1. 準(zhǔn)備工作
首先安裝必要的庫:
pip install requests beautifulsoup4
2. 基礎(chǔ)爬蟲實現(xiàn)
import requests
from bs4 import BeautifulSoup
import time
import random
def get_csdn_articles(keyword, pages=1):
"""
抓取CSDN上指定關(guān)鍵詞的文章
:param keyword: 搜索關(guān)鍵詞
:param pages: 要抓取的頁數(shù)
:return: 文章列表,包含標(biāo)題、鏈接、簡介等信息
"""
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'
}
base_url = "https://so.csdn.net/so/search"
articles = []
for page in range(1, pages + 1):
params = {
'q': keyword,
't': 'blog',
'p': page
}
try:
response = requests.get(base_url, headers=headers, params=params)
response.raise_for_status()
soup = BeautifulSoup(response.text, 'html.parser')
items = soup.find_all('div', class_='search-item')
for item in items:
title_tag = item.find('a', class_='title')
if not title_tag:
continue
title = title_tag.get_text().strip()
link = title_tag['href']
# 獲取簡介
desc_tag = item.find('p', class_='content')
description = desc_tag.get_text().strip() if desc_tag else '無簡介'
# 獲取閱讀數(shù)和發(fā)布時間
info_tags = item.find_all('span', class_='date')
read_count = info_tags[0].get_text().strip() if len(info_tags) > 0 else '未知'
publish_time = info_tags[1].get_text().strip() if len(info_tags) > 1 else '未知'
articles.append({
'title': title,
'link': link,
'description': description,
'read_count': read_count,
'publish_time': publish_time
})
print(f"已抓取第 {page} 頁,共 {len(items)} 篇文章")
# 隨機(jī)延遲,避免被封
time.sleep(random.uniform(1, 3))
except Exception as e:
print(f"抓取第 {page} 頁時出錯: {e}")
continue
return articles
if __name__ == '__main__':
# 示例:抓取關(guān)于"Python爬蟲"的前3頁文章
keyword = "
3. 高級功能擴(kuò)展
3.1 抓取文章詳情
def get_article_detail(url):
"""抓取文章詳情內(nèi)容"""
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'
}
try:
response = requests.get(url, headers=headers)
response.raise_for_status()
soup = BeautifulSoup(response.text, 'html.parser')
# 獲取文章主體內(nèi)容
content = soup.find('article')
if content:
# 清理不必要的標(biāo)簽
for tag in content(['script', 'style', 'iframe', 'nav', 'footer']):
tag.decompose()
return content.get_text().strip()
return "無法獲取文章內(nèi)容"
except Exception as e:
print(f"抓取文章詳情出錯: {e}")
return None3.2 保存數(shù)據(jù)到文件
import json
import csv
def save_to_json(data, filename):
"""保存數(shù)據(jù)到JSON文件"""
with open(filename, 'w', encoding='utf-8') as f:
json.dump(data, f, ensure_ascii=False, indent=2)
def save_to_csv(data, filename):
"""保存數(shù)據(jù)到CSV文件"""
if not data:
return
keys = data[0].keys()
with open(filename, 'w', newline='', encoding='utf-8') as f:
writer = csv.DictWriter(f, fieldnames=keys)
writer.writeheader()
writer.writerows(data)
4. 完整示例
if __name__ == '__main__':
# 抓取文章列表
keyword = "Python爬蟲"
articles = get_csdn_articles(keyword, pages=2)
# 抓取前3篇文章的詳情
for article in articles[:3]:
article['content'] = get_article_detail(article['link'])
time.sleep(random.uniform(1, 2)) # 延遲
# 保存數(shù)據(jù)
save_to_json(articles, 'csdn_articles.json')
save_to_csv(articles, 'csdn_articles.csv')
print("數(shù)據(jù)抓取完成并已保存!")
5. 反爬蟲策略應(yīng)對
1.設(shè)置請求頭:模擬瀏覽器訪問
2.隨機(jī)延遲:避免請求過于頻繁
3.使用代理IP:防止IP被封
4.處理驗證碼:可能需要人工干預(yù)
5.遵守robots.txt:尊重網(wǎng)站的爬蟲規(guī)則
到此這篇關(guān)于一文教你Python如何快速精準(zhǔn)抓取網(wǎng)頁數(shù)據(jù)的文章就介紹到這了,更多相關(guān)Python抓取網(wǎng)頁數(shù)據(jù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 利用Python抓取網(wǎng)頁數(shù)據(jù)的多種方式與示例詳解
- Python使用BeautifulSoup和Scrapy抓取網(wǎng)頁數(shù)據(jù)的具體教程
- Python使用BeautifulSoup抓取和解析網(wǎng)頁數(shù)據(jù)的操作方法
- Python爬蟲之使用BeautifulSoup和Requests抓取網(wǎng)頁數(shù)據(jù)
- 淺談如何使用python抓取網(wǎng)頁中的動態(tài)數(shù)據(jù)實現(xiàn)
- Python獲取網(wǎng)頁數(shù)據(jù)的五種方法
- Python實現(xiàn)快速抓取網(wǎng)頁數(shù)據(jù)的5種高效方法
相關(guān)文章
Python 實現(xiàn)一個簡單的web服務(wù)器
這篇文章主要介紹了Python 實現(xiàn)一個簡單的web服務(wù)器的方法,幫助大家更好的理解和學(xué)習(xí)python,感興趣的朋友可以了解下2021-01-01
python?selenium實現(xiàn)登錄豆瓣示例詳解
大家好,本篇文章主要講的是python?selenium登錄豆瓣示例詳解,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下2022-01-01
最新PyCharm從安裝到PyCharm永久激活再到PyCharm官方中文漢化詳細(xì)教程
這篇文章涵蓋了最新版PyCharm安裝教程,最新版PyCharm永久激活碼教程,PyCharm官方中文(漢化)版安裝教程圖文并茂非常詳細(xì),需要的朋友可以參考下2020-11-11
Python中關(guān)于集合的介紹與常規(guī)操作解析
Python除了List、Tuple、Dict等常用數(shù)據(jù)類型外,還有一種數(shù)據(jù)類型叫做集合(set),集合的最大特點是:集合里邊的元素是不可重復(fù)的并且集合內(nèi)的元素還是無序的2021-09-09
一文搞懂Python中pandas透視表pivot_table功能
透視表是一種可以對數(shù)據(jù)動態(tài)排布并且分類匯總的表格格式。或許大多數(shù)人都在Excel使用過數(shù)據(jù)透視表,也體會到它的強(qiáng)大功能,而在pandas中它被稱作pivot_table,今天通過本文給大家介紹Python中pandas透視表pivot_table功能,感興趣的朋友一起看看吧2021-11-11

