文章目录
- 代码
- go
- python
在 go语言中调用 python 程序,你可能会用到
代码
亲测
go
测试 go 文件
func TestR(t *testing.T) {// 设置要执行的Python脚本和参数scriptPath := "../nansen.py"arg1 := "nansen"// 执行Python脚本cmd := exec.Command("python", scriptPath, arg1)// 获取命令输出output, err := cmd.CombinedOutput()if err != nil {fmt.Println("Error:", err)return}// 打印Python脚本的输出fmt.Println(string(output))
}
python
nansen.py
import sys
from curl_cffi import requests# # 注意这个 impersonate 参数,指定了模拟哪个浏览器
# r = requests.get("https://portfolio.nansen.ai/dashboard/binance", impersonate="chrome101")# print(r.content)def nansen(name):print(name)r = requests.get("https://portfolio.nansen.ai/dashboard/binance", impersonate="chrome101")print(r.content)return r.contentif __name__ == "__main__":if len(sys.argv) < 2:print("Usage: nansen.py <name>")sys.exit(1)name = sys.argv[1]result = nansen(name)print(result)