Python爬取数据并写入MySQL数据库

本文介绍了一个简单的Python脚本,该脚本使用BeautifulSoup从颜色代码网站抓取颜色名称及其对应的十六进制值,并将这些数据存入MySQL数据库。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先我们来爬取 http://html-color-codes.info/color-names/ 的一些数据。

这里写图片描述

F12ctrl+u 审查元素,结果如下:

这里写图片描述

  结构很清晰简单,我们就是要爬 tr 标签里面的 styletr 下几个并列的 td 标签,下面是爬取的代码:

#!/usr/bin/env python
# coding=utf-8

import requests
from bs4 import BeautifulSoup
import MySQLdb

print('连接到mysql服务器...')
db = MySQLdb.connect("localhost","hp","Hp12345.","TESTDB")
print('连接上了!')
cursor = db.cursor()
cursor.execute("DROP TABLE IF EXISTS COLOR")
sql = """CREATE TABLE COLOR (
        Color CHAR(20) NOT NULL,
        Value CHAR(10),
        Style CHAR(50) )"""

cursor.execute(sql)

hdrs = {'User-Agent':'Mozilla/5.0 (X11; Fedora; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko)'}

url = "http://html-color-codes.info/color-names/"

r = requests.get(url, headers = hdrs)
soup = BeautifulSoup(r.content.decode('gbk', 'ignore'), 'lxml')
trs = soup.find_all('tr')   # 获取全部tr标签成为一个列表
for tr in trs:              # 遍历列表里所有的tr标签单项
    style = tr.get('style') # 获取每个tr标签里的属性style
    tds = tr.find_all('td') # 将每个tr标签下的td标签获取为列表
    td = [x for x in tds]   # 获取的列表
    name = td[1].text.strip()       # 直接从列表里取值
    hex = td[2].text.strip()
    # print u'颜色: ' + name + u'颜色值: '+ hex + u'背景色样式: ' + style
    # print 'color: ' + name + '\tvalue: '+ hex + '\tstyle: ' + style
    insert_color = ("INSERT INTO COLOR(Color,Value,Style)" "VALUES(%s,%s,%s)")
    data_color = (name, hex, style)
    cursor.execute(insert_color, data_color)
    db.commit()
    # print '******完成此条插入!' 

print '爬取数据并插入mysql数据库完成...'

运行结果:
这里写图片描述

$ mysql -u hp -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 28
Server version: 5.7.17 MySQL Community Server (GPL)

Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use TESTDB
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from COLOR;
+----------------------+--------+----------------------------------------+
| Color                | Value  | Style                                  |
+----------------------+--------+----------------------------------------+
| IndianRed            | CD5C5C | background-color:indianred;            |
| LightCoral           | F08080 | background-color:lightcoral;           |
| Salmon               | FA8072 | background-color:salmon;               |
| DarkSalmon           | E9967A | background-color:darksalmon;           |
| LightSalmon          | FFA07A | background-color:lightsalmon;          |
| Crimson              | DC143C | background-color:crimson;              |
| Red                  | FF0000 | background-color:red;                  |
| FireBrick            | B22222 | background-color:fireBrick;            |
| DarkRed              | 8B0000 | background-color:darkred;              |
| Pink                 | FFC0CB | background-color:pink;                 |
| LightPink            | FFB6C1 | background-color:lightpink;            |
| HotPink              | FF69B4 | background-color:hotpink;              |
| DeepPink             | FF1493 | background-color:deeppink;             |

...

| AntiqueWhite         | FAEBD7 | background-color:antiquewhite;         |
| Linen                | FAF0E6 | background-color:linen;                |
| LavenderBlush        | FFF0F5 | background-color:lavenderblush;        |
| MistyRose            | FFE4E1 | background-color:mistyrose;            |
| Gainsboro            | DCDCDC | background-color:gainsboro;            |
| LightGrey            | D3D3D3 | background-color:lightgrey;            |
| Silver               | C0C0C0 | background-color:silver;               |
| DarkGray             | A9A9A9 | background-color:darkgray;             |
| Gray                 | 808080 | background-color:gray;                 |
| DimGray              | 696969 | background-color:dimgray;              |
| LightSlateGray       | 778899 | background-color:lightslategray;       |
| SlateGray            | 708090 | background-color:slategray;            |
| DarkSlateGray        | 2F4F4F | background-color:darkslategray;        |
| Black                | 000000 | background-color:black;                |
+----------------------+--------+----------------------------------------+
143 rows in set (0.00 sec)
<think>我们正在讨论如何将爬取数据存入数据库。根据引用内容,我们可以总结出以下关键步骤: 1. 连接数据库:需要提供数据库的地址、端口、用户名、密码、数据库名以及字符编码。 2. 创建游标:通过连接对象创建游标,用于执行SQL语句。 3. 执行SQL语句:包括创建表(如果需要)和插入数据。 4. 提交事务:对于增删改操作,需要提交事务以使更改生效。 5. 关闭连接:操作完成后关闭游标和数据库连接。 引用中提供了两种不同的数据库操作方式:一种是使用MySQLdb(引用[2]),另一种是使用pymysql(引用[3])。MySQLdb是Python2时代常用的库,而pymysqlPython3中更常用的MySQL连接库。 下面我们将分别使用pymysqlMySQLdb(如果环境支持)来演示如何将爬取数据存入数据库。 步骤: 1. 安装数据库驱动:如果使用pymysql,则安装pymysql;如果使用MySQLdb,则安装mysqlclient(MySQLdb的Python3兼容版本)。 2. 建立数据库连接。 3. 创建游标对象。 4. 执行SQL语句(创建表、插入数据等)。 5. 提交事务。 6. 关闭游标和连接。 示例1:使用pymysql 首先安装pymysql:`pip install pymysql` 然后参考以下代码: ```python import pymysql # 1. 连接数据库 db = pymysql.connect( host="数据库地址", port=3306, # 端口,默认为3306 user="用户名", password="密码", database="数据库名", charset='utf8mb4' # 一般使用utf8mb4以支持存储Emoji表情 ) # 2. 创建游标 cursor = db.cursor() # 3. 执行SQL语句 # 创建表(如果表不存在) create_table_sql = """ CREATE TABLE IF NOT EXISTS articles ( id INT AUTO_INCREMENT PRIMARY KEY, title VARCHAR(255) NOT NULL, url VARCHAR(255) NOT NULL, tags VARCHAR(255), create_date DATE, content TEXT ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; """ cursor.execute(create_table_sql) # 假设我们有一个item,包含需要插入的数据 item = { 'title': '示例标题', 'url': 'https://example.com', 'tags': '示例,标签', 'create_date': '2023-01-01', 'content': '这是文章内容' } # 插入数据的SQL语句 insert_sql = """ INSERT INTO articles (title, url, tags, create_date, content) VALUES (%s, %s, %s, %s, %s) """ # 参数列表 params = ( item['title'], item['url'], item['tags'], item['create_date'], item['content'] ) try: cursor.execute(insert_sql, params) db.commit() # 提交事务 print("数据插入成功") except Exception as e: print(f"插入数据时发生错误: {e}") db.rollback() # 发生错误时回滚 finally: # 关闭游标和连接 cursor.close() db.close() ``` 示例2:使用MySQLdb(或mysqlclient) 安装:`pip install mysqlclient` 代码与pymysql类似,但注意MySQLdb在连接时参数名略有不同,且不需要指定`port`为关键字参数(可以直接在host后面用冒号指定端口,或者使用port关键字参数)。 ```python import MySQLdb # 1. 连接数据库 db = MySQLdb.connect( host="数据库地址", port=3306, # 注意:MySQLdb中port参数是有效的 user="用户名", passwd="密码", # 注意参数名是passwd db="数据库名", charset='utf8mb4', use_unicode=True ) # 2. 创建游标 cursor = db.cursor() # 3. 执行SQL语句(同上,创建表和插入数据) # ... 省略,与pymysql示例中的SQL执行部分相同 # 注意:在MySQLdb中,插入数据时同样使用%s作为占位符,但参数传递方式相同。 # 最后记得提交、异常处理和关闭连接 ``` 注意事项: - 在插入数据时,使用参数化查询(%s作为占位符)可以防止SQL注入攻击。 - 字符编码建议使用utf8mb4,以支持存储Emoji等特殊字符。 - 对于日期类型的字段,确保传入的日期字符串格式正确,或者使用Python的datetime.date对象。 在实际的爬虫项目中,我们通常会将数据库操作封装到一个单独的类或者pipeline中(例如在Scrapy框架中,可以编写一个Item Pipeline来处理数据的存储)。 引用[2]中展示了一个Scrapy的Pipeline示例,其中在`__init__`方法中建立数据库连接,在`process_item`方法中处理每个item的插入操作。 总结:使用Python爬取数据存入数据库,主要步骤包括连接数据库、创建游标、执行SQL语句、提交和关闭连接。根据使用的数据库驱动不同(pymysqlMySQLdb),代码略有差异,但整体流程一致。 §§相关问题§§ 1. 如何防止在将爬取数据存入数据库时出现重复数据? 2. 如何处理数据库连接异常和超时问题? 3. 在Scrapy框架中如何编写Item Pipeline来存储数据数据库? 4. 除了MySQLPython还支持哪些数据库?如何操作?</think>### Python爬虫数据存入数据库的方法 将爬取数据存入数据库爬虫开发的关键步骤,以下是详细的操作流程和示例代码: #### 一、基本步骤 1. **安装数据库驱动** ```bash pip install pymysql # MySQL pip install pymongo # MongoDB ``` 2. **建立数据库连接** ```python import pymysql # 连接MySQL(参数根据实际修改) db = pymysql.connect( host="localhost", # 数据库地址 port=3306, # 端口 user="root", # 用户名 password="123456", # 密码 database="spider_db", # 数据库名 charset="utf8mb4" # 编码 ) cursor = db.cursor() # 创建游标 ``` [^1][^2] 3. **创建数据表** ```sql CREATE TABLE articles ( id INT AUTO_INCREMENT PRIMARY KEY, title VARCHAR(255) NOT NULL, url VARCHAR(255) NOT NULL UNIQUE, content TEXT, create_date DATE ); ``` 4. **插入数据** ```python item = { 'title': 'Python教程', 'url': 'https://example.com/python', 'content': 'Python基础语法...', 'create_date': '2023-06-15' } sql = """ INSERT INTO articles (title, url, content, create_date) VALUES (%s, %s, %s, %s) """ cursor.execute(sql, ( item['title'], item['url'], item['content'], item['create_date'] )) db.commit() # 提交事务 ``` 5. **关闭连接** ```python cursor.close() db.close() ``` #### 二、完整示例(MySQL) ```python import pymysql def save_to_mysql(item): try: db = pymysql.connect( host="localhost", user="root", password="123456", database="spider_db", charset="utf8mb4" ) cursor = db.cursor() sql = """INSERT INTO articles (title, url, content, create_date) VALUES (%s, %s, %s, %s)""" cursor.execute(sql, ( item['title'], item['url'], item['content'], item['create_date'] )) db.commit() print("数据插入成功") except Exception as e: print(f"数据库错误: {e}") db.rollback() finally: cursor.close() db.close() # 示例数据 article = { 'title': '爬虫技巧', 'url': 'https://example.com/spider', 'content': '数据存储方法...', 'create_date': '2023-06-18' } save_to_mysql(article) ``` #### 三、关键注意事项 1. **防重复机制** - 添加`UNIQUE`约束(如url字段) - 使用`ON DUPLICATE KEY UPDATE`语句 ```sql INSERT INTO ... ON DUPLICATE KEY UPDATE title=VALUES(title) ``` 2. **批量插入优化** ```python # 使用executemany提高效率 sql = "INSERT ... VALUES (%s,%s,%s,%s)" cursor.executemany(sql, [item1, item2, item3]) ``` 3. **错误处理** - 捕获`pymysql.Error`异常 - 网络超时设置:`connect(timeout=10)` - 使用连接池管理连接[^3] 4. **编码问题** - 统一使用`utf8mb4`编码 - 数据库数据表、连接三处编码需一致 #### 四、其他数据库 1. **MongoDB示例** ```python from pymongo import MongoClient client = MongoClient('localhost', 27017) db = client['spider_db'] collection = db['articles'] collection.insert_one({ 'title': 'MongoDB存储', 'url': 'https://example.com/mongodb', 'content': 'NoSQL数据库...' }) ``` 2. **SQLite示例** ```python import sqlite3 conn = sqlite3.connect('spider.db') cursor = conn.cursor() cursor.execute("CREATE TABLE ...") ``` #### 五、Scrapy集成(pipelines.py) ```python import pymysql class MysqlPipeline: def __init__(self): self.conn = pymysql.connect( host='localhost', user='root', password='123456', db='spider_db', charset='utf8mb4' ) self.cursor = self.conn.cursor() def process_item(self, item, spider): sql = """INSERT INTO articles (title, url, content) VALUES (%s, %s, %s)""" self.cursor.execute(sql, ( item['title'], item['url'], item['content'] )) self.conn.commit() return item def close_spider(self, spider): self.cursor.close() self.conn.close() ``` [^2] ---
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值