zoukankan      html  css  js  c++  java
  • python embedded in java example

    package com.jd.cis;
    import java.io.IOException;

    import javax.script.ScriptEngineManager;

    import org.apache.http.client.ClientProtocolException;
    import org.apache.http.client.ResponseHandler;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.impl.client.BasicResponseHandler;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.python.util.PythonInterpreter;
    public class UrlFetch {

        /**
         * @param args
         * @throws IOException
         * @throws ClientProtocolException
         */
        public static void main(String[] args) throws ClientProtocolException, IOException {
            // TODO Auto-generated method stub
    //        HttpClient httpclient = new DefaultHttpClient();
    //        try {
    //            String url="";
    //            url="http://www.yuncheng.com/searchcate.aspx?page=4&cate=%E5%9B%BE%E4%B9%A6&subcate=%E5%8E%9F%E5%88%9B%E6%96%87%E5%AD%A6&level3=%E7%8E%84%E5%B9%BB&level4=%E4%B8%9C%E6%96%B9%E7%8E%84%E5%B9%BB#hd";
    //            HttpGet httpget = new HttpGet(url);
    //            HttpClientParams.setCookiePolicy(httpclient.getParams(), CookiePolicy.BROWSER_COMPATIBILITY);
    //            System.out.println("executing request " + httpget.getURI());
    //
    //            // Create a response handler
    //            ResponseHandler<String> responseHandler = new BasicResponseHandler();
    //            String responseBody = httpclient.execute(httpget, responseHandler);
    //            System.out.println("----------------------------------------");
    //            System.out.println(responseBody);
    //            System.out.println("----------------------------------------");
    //
    //        } finally {
    //            // When HttpClient instance is no longer needed,
    //            // shut down the connection manager to ensure
    //            // immediate deallocation of all system resources
    //            httpclient.getConnectionManager().shutdown();
    //        }
           
           
            PythonInterpreter interpreter = new PythonInterpreter();
            String code="";
            code="url='http://www.yuncheng.com/searchcate.aspx?page=4&cate=%E5%9B%BE%E4%B9%A6&subcate=%E5%8E%9F%E5%88%9B%E6%96%87%E5%AD%A6&level3=%E7%8E%84%E5%B9%BB&level4=%E4%B8%9C%E6%96%B9%E7%8E%84%E5%B9%BB#hd'\nimport urllib2\nfor i in xrange(2,30):\n    print i\n    html=urllib2.urlopen(url).read()\n    f=open('c:/%s.txt'%i,'w')\n    f.write(html)\n    f.close()\n";
            interpreter.exec(code);
           
        }

    }

    original python script

    #!/usr/bin/env python
    #encoding=utf-8
    url="http://www.yuncheng.com/searchcate.aspx?page=4&cate=%E5%9B%BE%E4%B9%A6&subcate=%E5%8E%9F%E5%88%9B%E6%96%87%E5%AD%A6&level3=%E7%8E%84%E5%B9%BB&level4=%E4%B8%9C%E6%96%B9%E7%8E%84%E5%B9%BB#hd "
    url=url.strip()
    import urllib2
    for i in xrange(2,30):
        print i
        html=urllib2.urlopen(url).read()
        f=open("c:/22/%s.txt"%i,"w")
        f.write(html)
        f.close()

  • 相关阅读:
    java编程基础--方法
    MySQL中使用LIMIT进行分页的方法
    Java编程基础--数据类型
    Java开发入门
    SpringBoot实战项目(十七)--使用拦截器实现系统日志功能
    SpringBoot实战项目(十六)--拦截器配置及登录拦截
    SpringBoot实战项目(十五)--修改密码及登录退出功能实现
    SpringBoot实战项目(十四)--登录功能之登录表单验证
    PHP setcookie 网络函数
    PHP mysqli_kill MySQLi 函数
  • 原文地址:https://www.cnblogs.com/lexus/p/2374586.html
Copyright © 2011-2022 走看看