我有一個解析HTML頁面的python服務器,腳本運行良好。不過,我有一個android應用程序,將通過發送參數即URL來調用此服務器。如何在python中執行POST/GET請求?
我想讓服務器獲取android應用程序發佈的URL並解析HTML頁面的數據。
我的查詢是: - 我應該使用哪種方法? GET或POST
我經歷了教程,我認爲這是POST方法。
下面是我製作的腳本/服務器。請告訴我應該做些什麼編輯。
import cherrypy
import ConfigParser
import json
import mimetypes
import os
from jinja2 import Environment, FileSystemLoader
from bs4 import BeautifulSoup
import requests
import urlparse
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
import SocketServer
########################################################################
details_array=[]
small_details_array=[]
price_cell_array=[]
lst = []
URL_path
class S(BaseHTTPRequestHandler):
def do_GET(self):
self._set_headers()
URL_path = urlparse.urlparse(self.path)
request_id = URL_path.path
def do_POST(self):
self._set_headers()
URL_path = urlparse.urlparse(self.path)
request_id = URL_path.path
r = requests.get(URL_path)#the URL_path holds the URL
data = r.text
soup = BeautifulSoup(data,"html.parser")
table = soup.find('table',{'class':'table'})
s=""
targetFile=open("plist","w")
detailtext = table.findAll('div',{'class':'detailtext'})
for det in detailtext:
details_array.append(det.text)
smalldetails = table.findAll('div',{'style':'padding-top:5px'})
for smallDet in smalldetails:
small_details_array.append(smallDet.text);
price_cells = table.findAll('td', {'class': 'pricecell'})
for price_cell in price_cells:
price_cell_array.append(price_cell.text)
for i in range(len(details_array)):
d_arr = {}
d_arr['detail']=details_array[i]
temp = small_details_array[i].split('\n')
d_arr['talktime'] = temp[1]
d_arr['keyword']=temp[3]
tempnew = price_cell_array[i].split('\n')
d_arr['price'] = tempnew[1]
d_arr['validity'] = tempnew[3]
# global list
lst.append(d_arr)
t_arr={}
t_arr['events'] = lst;
print json.dumps(t_arr)
targetFile.write("[TopUpList]"+"\n"+"events=")
targetFile.write(json.dumps(t_arr))
targetFile.write('\n[culturalEvents]\nevents={"events": [{"venue": "bangalore", "name": "Culttest"}]}')
targetFile.close()
#########################################################################
class Server():
@cherrypy.expose
def index(self):
return "Seems Like You're Lost :D"
@cherrypy.expose
def eventsList(self,choice):
message="Success, Event List Obtained"
status_code=0;
events=[]
try:
if choice.title() == "Cultural":
events = cultural_event_list['events']
elif choice.title() == "Prodlisting":
events = lists['events']
else:
status_code=-1
message="Failed, No Such Event Type Enlisted"
except:
status_code=-1
message="Failed, Server Error! Error Occured while retreiving Event List"
return json.dumps({'status_code':status_code,'message':message,'events':events})
@cherrypy.expose
def eventsStatus(self,choice):
message="Success, Event List Obtained"
status_code=0;
events=[]
try:
if choice.title() == "Cultural":
events = cultural_event_list['events']
elif choice.title() == "Prodlisting":
events = lists['events']
else:
status_code=-1
message="Failed, No Such Event Type Enlisted"
except:
status_code=-1
message="Failed, Server Error! Error Occured while retreiving Event List"
return json.dumps({'status_code':status_code,'message':message,'hash':json.dumps(events).__hash__()})
if __name__ == '__main__':
''' Setting up the Server with Specified Configuration'''
'''
config = ConfigParser.RawConfigParser()
config.read('server.conf')
cherrypy.server.socket_host = config.get('server','host')
cherrypy.server.socket_port = int(config.get('server','port'))
cherrypy.server.socket_host = '127.0.0.1'
cherrypy.server.socket_port = 5000
'''
list = ConfigParser.RawConfigParser()
cherrypy.config.update({'server.socket_host': '0.0.0.0',})
cherrypy.config.update({'server.socket_port': int(os.environ.get('PORT', '5000')),})
list.read('plist')#the file from where it reads
lists=json.loads(list.get('TopUpList','events'))
cultural_event_list=json.loads(list.get('culturalEvents','events'))
cherrypy.quickstart(Server())
P.S.我認爲android部分需要發送到python服務器的POST請求,如果我錯了,請和我糾正。