Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report