Java invokes Python and passes parameters (Crawler 8) __ios

Source: Internet
Author: User
Tags python script
antecedent

I did learn a lot recently.
In two aspects of the deepest feeling: a development of the app, the former to the back of the process is so ah. Every language has its strength, not whether it can be realized, but what you want to achieve. Joint adjustment Programme

The crawler has stored the data in MySQL this way.
But and backstage has not thought well how to adjust.

Idea One:
The Android group directly provides the user information needed by the reptile group.
How to give. HTTP protocol. Anyway, I don't know ...

Idea two:
Android is equivalent to the front end, it backstage is the backstage small brother.
Directly to the background to save the user information into a table.
Reads each user information, invokes the reptile group's Python program, the parameter passes in.
The reptile group directly stored the data back into the database in the background.

The idea should be yes, after all, Java and Python are powerful. java invoke Python

General idea:

When new users join or new data needs to be updated, Java invokes the crawler directly and passes in the parameters. The result is that the data is stored in the database.

Learn that Java has runtime.getruntime (). EXEC ()
That would be a lot better.
This approach is actually like executing the Python program on the cmd command line:

Runtime.getruntime (). EXEC ("Python d:\\studentdb.py id password");

IDs and password are the parameters passed in.
Change the original crawler program to:

if __name__ = = ' __main__ ':
    # Initializes the reptile object
    xs = xdspiderstudent ()
    # login (where the correct personal number and password information is passed in)
    #从Java里传入参数
    Xs.login ((Sys.argv[1]), (sys.argv[2])
    XS. Store ()
    xs.savemysql ()

You can do that.

This will require a server as well.
Just don't know how to deploy, still need to learn. Consequences

have tried to invoke Python in Java before, when the real use of the discovery is a black dragon. Error

The error is that I have too many programs, the program naming confusion, resulting in the execution of a program that should not be executed, and I thought it ran through.

This method:

Runtime.getruntime (). EXEC ("Python d:\\studentdb.py id password");  

can not pass formal parameters ...
You can use this only:

This allows you to pass the school number and password into the Python file and execute it.
and the former formal parameter method, is directly to the formal parameters as a string directly into the python ...
This means that the method can only pass strings.
The truth that was transferred to midnight a few days ago.

So, is there a solution?
There must be, either I don't know, or I won't. New Method Jython

Find Data discovery This third party package Jython is used to pass Python parameters and execute code.
This method was noted before, but due to its complexity, and the simplicity of the Runtime.getruntime () Exec () method, there is no thinking here, after all, programmers are the laziest.

First you have to download Jython this third party package. Debug Records

1,

console:failed to install ': java.nio.charset.UnsupportedCharsetException:cp0.

Run as>run configurations, select the second tab arguments, add-dpython.console.encoding=utf-8 to the VM arguments, and then apply>run the line.
2, Java use Jython.jar invoke Python script, the script import the third party library, error: Import xxx:no module named XXX.
Two ways to solve this problem:
(1) Download or PIP install required Third-party libraries, Jython.jar to Jython.zip, and decompression, the download of Third-party libraries (defined by the Library name folder) under the Jython folder under the/lib or/lib/site-packages, Finally, repackage the Jython folder (Note: Do not package the Jython folder directly, pack all the files in the Jython root directory), and change the suffix to. jar and configure it to the Java Engineering Directory (build path).
(2) Configure the Python system path, Java parsing script will go to Sys.path to find the import of Third-party libraries, as long as the Sys.path can find a Third-party library folder.
Note: In the Java end through the statement: pysystemstate sys = py.getsystemstate ();
System.out.println (Sys.path.toString ()), printing path information. the last Bug and repentance

A step-by-step will each encounter the bug break, encountered the biggest obstacle: can not identify requests package.
And the team-mates thought it might be related to the Python version.
We are unified using PYTHON35, from the name of this third package can be seen Jython2.7 can see it is to support the Python27.

But the development of things is not to go at the forefront of it.
There must be a solution to this problem. Just I don't know and won't.
There are two of the most stupid solutions now:
1. Rewrite the Python3 procedure for Python2
2. Backstage will obtain the student number password to become the CSV file to save to the local, then executes the Python program directly. The Python program reads the local CSV file, gets the number password, carries on the reptile activity, saves the data to MySQL, and then deletes the CSV.

The first method takes time.
The second method is too stupid to be able to collapse when dealing with multiple concurrent activities. and inefficient.

After thinking and thinking, in order to work smoothly, or choose the first kind of it, re-write Python program for 2.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.