Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Several lines of python code to solve the association of related words

Shulou Source: shulou.com Published: 2022-06-03 06:20:06 09月13日 Update

The problem of association of related words is often encountered in daily life, that is to say, entering a word and querying the relevant words does not sound too difficult, but how to accumulate so many words, and then use a good algorithm to connect the relevant content, itself is not simple. The author believes that the easiest way is to call the relevant interface, save a lot of trouble, a few lines of python code can be done.

#-*-coding: utf-8-*-# flake8: noqa__author__ = 'wukong'import urllibfrom urllib import urlencode# configure the appKey and openIdapp_key= you applied for "*" 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 ("% slots% s"% (request_url, params)) else: F = urllib.urlopen (request_url) Params) content = f.read () print contentdef main (): domain= "http://api.xiaocongjisuan.com/" servlet=" data/relativeword/mining "method=" get "request_url=domain+servlet # dictionary params = {} params [" appKey "] = app_key params [" openId "] = open_id # variable params [" keyword "] =" preschool education "params [" degree "] = 1 params ["upLimit"] = 20 params ["tSort"] = "down" request_content (request_url Params,method) if _ _ name__ = ='_ main__': main ()

Of course, it can also be realized with nodejs.

Var http = require ('http'); var qs = require (' querystring'); / / configure your application for appKey and openIdapp_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 port 80 and https corresponds to port 443.Please users correct var servlet= "data/relativeword/mining"; var method= "get"; var request_url=domain+servlet; var params = {}; params ['appKey'] = app_key; params [' openId'] = open_id; / / variable part params ["keyword"] = "preschool education"; params ["degree"] = 1; params ["upLimit"] = 20 Params ["tSort"] = "down"; request_content (request_url,port,params,method);} main ()

You can click on me to check how to use other languages. Well, it's actually very simple, and it doesn't have to be too much.

Tags: Vocabulary preschool port or part change education configuration code that is users practices content methods parameters addresses dictionaries frequent meetings interfaces Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno vpn Docker Shulou Information macOS