python 調用RESTFul接口

本週須要將爬蟲爬下來的數據入庫,由於以前已經寫好PHP的接口的,能夠直接經過python調用PHP接口來實現,因此把方法總結一下。python

 

//python編碼問題,由於很久用,因此很容易出現laravel

# -*- coding: utf8 -*-
#!/usr/bin/pythonsql

import sys
reload(sys)
sys.setdefaultencoding('utf8') 數據庫

//python 鏈接數據庫json

import  MySQLdbide

conn = MySQLdb.connect(
host = "localhost",
port = 22,
user = "root",
passwd = "root",
db = "test",
charset = "utf8")函數

cur = conn.cursor()
sql = 'select title,url,publish_time from mp_articles'
cur.execute(sql)
info = cur.fetchall()fetch

cur.close()
conn.commit()
conn.close()編碼

 

//python 調用RESTFul 接口url

test_data = {'title':title,'srcUrl':srcUrl,'composeTime':composeTime}   #參數,以此種字典形式呈現
test_data_urlencode = urllib.urlencode(test_data)             #須要注意的是編碼問題,經過urllib.urlencode()對將要傳入的函數進行編碼
requrl = 'http://47.90.20.84/addArticleFromSpider'          #這是傳入的URL,相似laravel5中的route,須要在laravel 的controller中設置route
req = urllib2.Request(url = requrl,data =test_data_urlencode)  經過Request的方式入數據,好像是默認根據PHP中採用的POST/GET 等方式傳入數據

res_data = urllib2.urlopen(req) #對返回的數據進行解析
res = res_data.read()  #讀取返回的數據

 

//try...except 當返回的參數有異常是,爲了避免中斷程序的運行,須要用此方式來保證程序運行

try:
  test_data = {'title':title,'srcUrl':srcUrl,'composeTime':composeTime}
  test_data_urlencode = urllib.urlencode(test_data)
  requrl = 'http://47.90.20.84/addArticleFromSpider'
  req = urllib2.Request(url = requrl,data =test_data_urlencode)
  res_data = urllib2.urlopen(req)
  res = res_data.read()
  print "addArticleFromSpider():" + res
except urllib2.HTTPError:
  print "there is an error"
  pass   #跳過錯誤,不進行處理,直接繼續執行

 

完整代碼以下:

# -*- coding: <utf8> -*-
#!/usr/bin/python

import  MySQLdb
import datetime
import time
import urllib
import urllib2
import json
import sys  
reload(sys)  
sys.setdefaultencoding('utf8') 

conn = MySQLdb.connect(
	host = "localhost",
	port = 22,
	user = "",
	passwd = "",
	db = "",
	charset = "utf8")

cur = conn.cursor()
sql = 'select title,url,publish_time from mp_articles'
cur.execute(sql)
info = cur.fetchall()
#print len(info)
for row in info:
	#print len(row)
	title = row[0]
	srcUrl   = row[1]
	publish_Time = row[2]
	composeTime = time.mktime(publish_Time.timetuple())
	composeTime = str(composeTime)
	try:
		test_data = {'title':title,'srcUrl':srcUrl,'composeTime':composeTime}
		test_data_urlencode = urllib.urlencode(test_data)
		requrl = 'http://47.90.20.84/addArticleFromSpider'
		req = urllib2.Request(url = requrl,data =test_data_urlencode)
		res_data = urllib2.urlopen(req)
		res = res_data.read()
		print "addArticleFromSpider():" + res
	except urllib2.HTTPError:
		print "there is an error"
		pass

cur.close()
conn.commit()
conn.close()
相關文章
相關標籤/搜索