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()
相关文章
相关标签/搜索