A few simple python codes to complete Zhou Gong's dream interpretation function

Posted by Ashoar on Sat, 12 Oct 2019 16:35:05 +0200

The Duke of Zhou interprets dreams is a folk book which relies on people's dreams to prophesy good fortune and bad fortune. There are seven kinds of interpretations of dreams. This is part of a very traditional Chinese cultural system, but how do you use code to get and search the data of Zhou Gong Jiemeng? Generally speaking, the process of crawler acquiring data and index searching is very complicated. It takes a lot of technology and time to develop, so the best way is to call the interface directly. The author takes python as an example to realize this function.

# -*- coding: utf-8 -*-
# flake8: noqa
__author__ = 'wukong'

import urllib
from urllib import urlencode

#Configure the appKey and openId you applied for
app_key="***"
open_id="***"

"""
request_url Request address
params Request parameters
method Request method

"""
def request_content(request_url,params,method):
    params = urlencode(params)
    
    if method and method.lower() =="get":
        f = urllib.urlopen("%s?%s" % (request_url, params))
    else:
        f = urllib.urlopen(request_url, params)
 
    content = f.read()
    print content

   
def main():
    
    domain="http://api.xiaocongjisuan.com/";
    servlet="life/dream/analysis"
    method="get"
    request_url=domain+servlet
    
    #Dictionaries
    params ={}
    params["appKey"]=app_key
    params["openId"]=open_id
    
    #Variable part
    params["keyword"]="woman"
    params["upLimit"]=20
    params["highligth"]=1
    
    request_content(request_url,params,method)
    
if __name__ == '__main__':
    main()

Friends who like other languages can order View here Next, attach a nodejs implementation:

var http = require('http'); 
var qs = require('querystring'); 

//Configure the appKey and openId you applied for
app_key = "***";
open_id = "***";
 
function request_content(request_url,port,params,method){
    
    var path=request_url;
    if(!!params){
        var content = qs.stringify(params);  
        path=request_url+'?' + content;
    }
    
    var options = { 
        port: port,
        path: path,
        method: method
    }; 
    
    if(method.toLowerCase()=='post'){
        options['headers']="Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8";
    }

    var req = http.request(options, function (res) { 
        res.setEncoding('utf8'); 
        res.on('data', function (chunk) { 
            console.log(chunk); 
        }); 
    });  
    
    req.on('error', function (e) { 
        console.log('problem with request: ' + e.message); 
    }); 
    
    req.end();
}

function main(){

    var domain="http://api.xiaocongjisuan.com/";
    var port=8080;//http corresponds to 80 ports, https corresponds to 443 ports, please users correct themselves
    var servlet="life/dream/analysis";
    var method="get";
    var request_url=domain+servlet;

    var params = {}; 
    params['appKey']=app_key;
    params['openId']=open_id;
    
    //Variable part
    params["keyword"]="woman";
    params["upLimit"]=20;
    params["highligth"]=1;
    
    request_content(request_url,port,params,method);
}

main();

Topics: node.js Python