通过前面几篇文章的学习,我们已经能够使用Scrapy
框架写出一些常见的网络爬虫。在本章中,我们将使用Scrapy
框架,将爬取到的数据存储到数据库中。
与将数据写入文件一样,写入到数据库中也是通过pipelines.py
文件完成的
存储到MySQL
修改pipelines.py
后,代码如下:
import pymysql
class BlogPipeline(object):
def __init__(self):
self.conn = pymysql.connect(host='127.0.0.1', user='root', passwd='123456', db='colin-test', charset='utf8mb4')
def process_item(self, item, *args, **kwargs):
for i in range(len(item['title'])):
title = item['title']