1. 程式人生 > >python爬蟲:使用Mongodb資料庫儲存資料學習筆記

python爬蟲:使用Mongodb資料庫儲存資料學習筆記

# -*- coding: utf-8 -*-
"""
Created on Sat Oct 22 21:01:23 2016
@author: hhxsym
"""
import requests
import json
import os
import pymongo
import time

inpath="C:\\Users\\hhxsym\\Desktop\\課程群Python爬蟲"
inpath = unicode(inpath , "utf8")
os.chdir(inpath)  #不做編碼轉換後,中文路徑無法開啟,更改
#連線資料庫
client =pymongo.MongoClient('localhost'
, 27017) #連線資料庫 sense = client['sense'] #建立資料庫 url_list = sense['url_list'] #建立資料庫表 def get_page_list(page=1): now = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())) url = 'http://www.senseluxury.com/destinations_list/77' payload = {'page':page, 'callback':'jsonp'} responses = requests.get(url,payload) #
請求網頁,獲得響應的內容,requests.get(url地址,關鍵字url引數) #print responses.url print responses.status_code #print responses.text[6:-1] #列印json格式的字串” (1) wb_data = json.loads(responses.text[6:-1]) #將字串轉換為python的字典 (2) print type(responses.text), type(wb_data) #對比兩種型別 (1)(2)對比 #print json.dumps(wb_data, encoding='utf-8', ensure_ascii=False) #json.dumps
方法,變換成中文列印 #通過迴圈獲取資料 for i in wb_data['val']['data']: title = i['title'] url = 'http://www.senseluxury.com'+i['url'] #資料拼接,獲得我們想要的資料 id = i['id'] server=i['server'].replace(' ',' ').split() memo = i['memo'] price = i['price'] address = i['address'] subject =i['subject'] data = {'title':title, 'id':id, 'server':server,'memo':memo, 'prie':price, 'address':address, 'subject':subject, 'create_time':now} url_list.insert_one(data) #插入資料(字典) # 注:生成的資料列表中 _id是自動生成的 print title, url if __name__=='__main__': get_page_list(1) # 網頁json型別的檢視:瀏覽器 -> 右鍵 檢查 -> network -> XHR ->頁面觸發(跳轉頁面) -> name勾選-> Response ->檢視是否出現json格式字串 # http://jsoneditoronline.org/ 線上格式化網站,檢視接送巢狀格式