I tried to make the mechanize module work with GAE, but no luck. I used latest version of mechanize and the versions that are GAE specific from here Python Mechanize + GAEpython code.
What I want to do is to login to a website and submit a form.
Example code
import mechanize
def mech(uname,passw,txtto,msg开发者_C百科):
br = mechanize.Browser()
br.open("http://example.com")
br.select_form(nr=0)
br["username"]= uname
br["password"]= passw
# br.form
response = br.submit()
br.select_form(nr=0)
# print br.form
br["txt"] = txtto
br["message"] = msg
br.submit()
br.back()
With mechanize module i get
'str' object has no attribute 'fileno'
and with gaemechanize i get
app.mechanize.ClientForm.ControlNotFoundError
ControlNotFoundError: no control matching name 'txt'
The code is tested and works outside gae so this is not the problem. I am using kay framework if this helps.
It looks like you are trying to do this from within a Python GAE app. There are many restrictions on what non-flex-environment GAE code can do (e.g. restrictions on shelling out to other processes, etc.) that do not exist within Google Cloud Functions. I recommend you try this from a Google Cloud Function. While there is not (yet) official python support for GCF, there is a github project that will let you deploy python code to GCF.
I was able to get some similar browser automation code to work in GCF using a Node.js chromium remote plugin, so I would think GCF will work for you as well.
Using a cloud function will also take a lot of load away from your GAE app, so its an all around better solution, as this is really more of a background job.
Whether you decide to use Node.js or Python to write your cloud function, once it's set up you can easily call it from a Pub/Sub or HTTP trigger from within your Python GAE app.
https://cloud.google.com/functions/
精彩评论