python爬取数据时候,要请求第三方接口数据,这里总结下三种常用的请求方式
# -*- coding:utf-8 -*-
import requests
import pycurl
import StringIO
import urllib
import urllib2
# requests请求接口
class Request():
def __init__(self, url):
self.url = url
self.headers = {
'user-agent': 'my-app/0.0.1'
}
def get(self,params = {}):
r = requests.get(self.url, headers=self.headers,params = params,cookies={},timeout=0.1)
print('get request url ' + r.url)
if r.status_code == 200:
print(r.text)
def post(self,params = {}):
r = requests.post(self.url, headers=self.headers, data=params)
print('post request url ' + r.url)
if r.status_code == 200:
print(r.text)
class pycurlRequest():
def __init__(self, url):
self.url = url
self.headers = {
'user-agent': 'my-app/0.0.1'
}
def get(self,params = {}):
c = pycurl.Curl()
str = StringIO.StringIO()
c.setopt(pycurl.URL, self.url)
c.setopt(pycurl.WRITEFUNCTION, str.write)
c.setopt(pycurl.TIMEOUT, 10) # 连接超时设置
c.setopt(pycurl.USERAGENT,"Mozilla/5.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)") # 模拟浏览器
c.setopt(pycurl.FOLLOWLOCATION, 1)
c.perform()
if c.getinfo(pycurl.HTTP_CODE) == 200:
print(str.getvalue().decode('utf-8'))
def post(self,params = {}):
c = pycurl.Curl()
str = StringIO.StringIO()
c.setopt(pycurl.URL, self.url)
#c.setopt(pycurl.COOKIEJAR, cookiejar)
c.setopt(pycurl.WRITEFUNCTION, str.write)
c.setopt(pycurl.TIMEOUT, 10) # 连接超时设置
c.setopt(pycurl.USERAGENT,"Mozilla/5.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)") # 模拟浏览器
c.setopt(pycurl.FOLLOWLOCATION, 1)
postfields = urllib.urlencode(postdata)
c.setopt(c.POSTFIELDS, postfields)
c.perform()
if c.getinfo(pycurl.HTTP_CODE) == 200:
print(str.getvalue().decode('utf-8'))
class urllibRequest():
def __init__(self, url):
self.url = url
self.headers = {
'user-agent': 'my-app/0.0.1'
}
def get(self,params = {}):
params = urllib.urlencode(params)
req = urllib2.Request(self.url+'?'+params, headers=self.headers)
resp = urllib2.urlopen(req)
cont = resp.read().decode('utf8')
print(cont)
def post(self, params={}):
params = urllib.urlencode(params)
req = urllib2.Request(self.url,data=params, headers=self.headers)
resp = urllib2.urlopen(req)
cont = resp.read().decode('utf8')
print(cont)