1 自定义一条数据线管道
class readbookmysqlpipeline:
2 在settings里打开管道
”xx”:299,
3 在settings里设置mysql基本信息
DB_HOST:127.0.0.1
4 在pipeline里
import pymysql
from scrapy.utils.project import get_project_settings
class zz
def open_spider(self,spider)
settings = get_project_settings()
print(settings【“db_host“】)
self.conn = pymysql.Connect(...
self.cursor = self.conn.cursor()
def process_item()
sql=”write sql values(“{}“,“{}”).format(item【·sric·】,item【·sric·】)
self.conn.commit()
self.cursor.execute(sql)
return item
def close_spider()
self.cursor().close()
self.conn.close()
在spiders 里
from ..items import readbookItem
def parse_item():
book = readbookItem(src=src,alt=alt)
yield book