Python爬虫入门:从零到数据采集

Python爬虫基础指南

Python爬虫是自动化获取网络数据的技术,广泛应用于数据采集、市场分析等领域。以下是核心实现步骤:

1. 核心库选择
python 复制代码
import requests  # 发送HTTP请求
from bs4 import BeautifulSoup  # HTML解析
import pandas as pd  # 数据存储
2. 基础爬取流程
python 复制代码
# 发送请求
response = requests.get("https://example.com/books")
response.encoding = 'utf-8'  # 设置编码

# 解析HTML
soup = BeautifulSoup(response.text, 'html.parser')

# 数据提取示例
book_titles = [h2.text for h2 in soup.select('.book-title')]
book_prices = [float(div.text.strip('¥')) 
               for div in soup.select('.price')]

# 存储数据
df = pd.DataFrame({'书名': book_titles, '价格': book_prices})
df.to_csv('book_data.csv', index=False)
3. 关键技巧
  • 反爬应对

    python 复制代码
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)',
        'Cookie': 'sessionid=abc123'
    }
    response = requests.get(url, headers=headers)
  • 动态页面处理(使用Selenium):

    python 复制代码
    from selenium import webdriver
    driver = webdriver.Chrome()
    driver.get(url)
    dynamic_content = driver.find_element_by_class('js-loaded-data').text
4. 完整案例:豆瓣图书爬虫
python 复制代码
def douban_spider():
    url = "https://book.douban.com/top250"
    res = requests.get(url, headers={'User-Agent': 'Mozilla/5.0'})
    soup = BeautifulSoup(res.text, 'lxml')
    
    books = []
    for item in soup.select('.item'):
        title = item.select_one('.pl2 a')['title']
        rating = item.select_one('.rating_nums').text
        books.append((title, float(rating)))
    
    return pd.DataFrame(books, columns=['书名', '评分'])

df = douban_spider()
df.to_excel('豆瓣图书TOP250.xlsx')
5. 注意事项
  1. 遵守规则

    • 检查robots.txt(如https://site.com/robots.txt
    • 设置请求间隔:time.sleep(random.uniform(1,3))
  2. 异常处理

    python 复制代码
    try:
        response = requests.get(url, timeout=10)
    except (requests.ConnectionError, requests.Timeout) as e:
        print(f"请求失败: {str(e)}")
  3. 数据清洗

    python 复制代码
    # 去除空白字符
    clean_text = re.sub(r'\s+', ' ', raw_text).strip()

提示:对于复杂网站建议使用Scrapy框架,其内置的异步处理、管道机制和中间件能显著提升效率。

相关推荐
weixin_446260853 小时前
[特殊字符] 视觉Transformer (ViT) 原理及性能突破:从CNN到大规模自注意力机制的迁移
深度学习·cnn·transformer
小a彤3 小时前
GE 在 CANN 五层架构中的位置
人工智能·深度学习·transformer
碧海银沙音频科技研究院4 小时前
通话AEC与语音识别AEC的软硬回采链路
深度学习·算法·语音识别
放下华子我只抽RuiKe54 小时前
React 从入门到生产(四):自定义 Hook
前端·javascript·人工智能·深度学习·react.js·自然语言处理·前端框架
AI算法沐枫5 小时前
深度学习python代码处理科研测序数据
数据结构·人工智能·python·深度学习·决策树·机器学习·线性回归
初心未改HD6 小时前
深度学习之Attention注意力机制详解
人工智能·深度学习
code_pgf6 小时前
模态生成器:原理详解与推荐开源项目
人工智能·深度学习·开源
文歌子7 小时前
DeepEarth 深度解析:AI 如何理解地球的时空规律
深度学习
初心未改HD7 小时前
深度学习之Transformer架构详解
人工智能·深度学习·transformer