(八)使用mysql儲存爬取資料

來源:互聯網
上載者:User

標籤:菜單   mysq   使用   comm   charset   中文   def   code   令行   

一、從官網下載phpstudy與其他前期準備

  cmd中

pip install pymysql

  啟動phpstudy,apache和mysql運行後 “其他選項菜單”-“MySQL工具”-“MySQL命令列” 

  密碼為root

這是用的都是SQL語言,記得語句後加分號。

show databases;

接著建立表

create table good(id int(32) auto_increment primary key,title varchar(100),link varchar(100),comment varchar(100));

資料庫建立完畢

二、pipelines.py與資料庫進行連結

最後亂碼了 終於解決!

 

其中用到了

set names gbk;

它相當於下面的三句指令:

SET character_set_client = gbk;SET character_set_results = gbk;SET character_set_connection = gbk;

 這樣輸出的中文就不會亂碼了!

下給出完整pipelines.py

import pymysqlclass Dangdang2Pipeline(object):    def process_item(self, item, spider):        conn=pymysql.connect(host="127.0.0.1",user="root",passwd="root",db="dangdang",charset="utf8")        for i in range(len(item["title"])):            title=item["title"][i]            #.encode(‘utf8‘)            link=item["link"][i]            comment=item["comment"][i]            #print(title,link,comment)            sql="insert into good(title,link,comment) values (‘%s‘,‘%s‘,‘%s‘);"%(title,link,comment)            #print(sql)            conn.query(sql)        conn.close()        return item

 

(八)使用mysql儲存爬取資料

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.