【Python实战案例】爬虫项目实例(附赠源码)

文章目录

声明

请您遵守网站的robots文件规定,本文目的只是做学习交流使用,包括多个模块,例如数据存储、日志记录、错误处理、多线程或异步请求

安装必要的库

pip install requests beautifulsoup4 sqlite3

项目结构

创建以下文件和目录结构

my_crawler/

├── config.py

├── crawler.py

├── db.py

├── logger.py

└── main.py

技术细节

配置文件 config.py

配置文件用于存储常量和配置项:

python 复制代码
BASE_URL = 'https://example.com'
ARTICLES_URL = f'{BASE_URL}/articles'
DATABASE_NAME = 'articles.db'
LOG_FILE = 'crawler.log'

1.数据库操作 db.py

用于创建数据库表和插入数据:

python 复制代码
#db.py

import sqlite3
from config import DATABASE_NAME

def init_db():
    conn = sqlite3.connect(DATABASE_NAME)
    cursor = conn.cursor()
    cursor.execute('''
        CREATE TABLE IF NOT EXISTS articles (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            title TEXT NOT NULL,
            url TEXT NOT NULL
        )
    ''')
    conn.commit()
    conn.close()

def insert_article(title, url):
    conn = sqlite3.connect(DATABASE_NAME)
    cursor = conn.cursor()
    cursor.execute('INSERT INTO articles (title, url) VALUES (?, ?)', (title, url))
    conn.commit()
    conn.close()

2.日志记录 logger.py

用于配置日志记录:

python 复制代码
logger.py

import logging
from config import LOG_FILE

def setup_logger():
    logging.basicConfig(
        filename=LOG_FILE,
        level=logging.INFO,
        format='%(asctime)s - %(levelname)s - %(message)s'
    )

logger = setup_logger()

3.爬虫逻辑 crawler.py

包含爬虫的主要逻辑:

python 复制代码
 crawler.py

import requests
from bs4 import BeautifulSoup
from config import ARTICLES_URL
from db import insert_article
from logger import logger

def fetch_page(url):
    """获取网页内容"""
    try:
        response = requests.get(url)
        response.raise_for_status()  # 检查请求是否成功
        return response.text
    except requests.RequestException as e:
        logger.error(f"请求错误: {e}")
        return None

def parse_html(html):
    """解析HTML,提取文章标题和URL"""
    soup = BeautifulSoup(html, 'html.parser')
    articles = []
    for article in soup.find_all('div', class_='article'):
        title = article.find('h1', class_='article-title').get_text(strip=True)
        url = article.find('a')['href']
        articles.append((title, url))
    return articles

def crawl_articles():
    """抓取并保存文章"""
    html = fetch_page(ARTICLES_URL)
    if html:
        articles = parse_html(html)
        for title, url in articles:
            insert_article(title, url)
            logger.info(f"已保存文章: {title} - {url}")
  1. 主程序 main.py
    启动爬虫的主程序:
python 复制代码
 main.py

from crawler import crawl_articles
from db import init_db
from logger import logger

def main():
    logger.info("开始爬虫任务")
    init_db()
    crawl_articles()
    logger.info("爬虫任务完成")

if __name__ == '__main__':
    main()

小结

请您遵守网站的robots文件规定,本文目的只是做学习交流使用,感谢csdn平台

相关推荐
overmind19 分钟前
oeasy Python 116 用列表乱序shuffle来洗牌抓拍玩升级拖拉机
服务器·windows·python
念越19 分钟前
MySQL报错:Column count doesn‘t match value count at row 1 解决方案(超详细)
数据库·mysql
A懿轩A27 分钟前
【Java 基础编程】Java 枚举与注解从零到一:Enum 用法 + 常用注解 + 自定义注解实战
java·开发语言·python
SmartBrain44 分钟前
FastAPI实战(第二部分):用户注册接口开发详解
数据库·人工智能·python·fastapi
开发者导航1 小时前
【开发者导航】多功能生成模型开发工具:Diffusers 详细介绍
人工智能·python·学习·macos·信息可视化
ん贤1 小时前
Scrapy 嵌入 FastAPI 的坑:Asyncio/Twisted 桥接 + 代理池设计
python·scrapy·fastapi
岱宗夫up1 小时前
从代码模式到智能模式:AI时代的设计模式进化论
开发语言·python·深度学习·神经网络·自然语言处理·知识图谱
xzjiang_3651 小时前
Jupyter 运行经验3:读入和显示一张图片
ide·python·jupyter
先做个垃圾出来………1 小时前
DeepDiff
python
喵手1 小时前
Python爬虫实战:节奏律动 - Billboard Hot 100 历史榜单深度采集实战!
爬虫·python·爬虫实战·零基础python爬虫教学·billboard hot·历史版单采集·采集billboard hot