Cannot manipulate inputs in form using robobrowser - forms

I've been at this for literally a day; none of the Youtube videos go beyond a very basic example. Please help. I'm sure Im missing something really basic here.
Would it change things if the input boxes are embedded in a table? Here is my code:
from robobrowser import RoboBrowser
br = RoboBrowser(history=True, parser = 'html.parser', user_agent='Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11')
br.open('https://fbcad.org/Advanced-Search')
form = br.get_form(id='Form')
form['NameFirst'] = "john"
form['NameLast'] = "smith"
br.submit_form(form)
here is the error:
C:\Python\Python37\python.exe C:/Python/Python37/FBCAD.py
Traceback (most recent call last):
File "C:/Python/Python37/FBCAD.py", line 7, in <module>
form['NameFirst'] = "john"
File "C:\Python\Python37\lib\site-packages\robobrowser\forms\form.py", line 216, in __setitem__
self.fields[key].value = value
File "C:\Python\Python37\lib\site-packages\werkzeug\datastructures.py", line 784, in __getitem__
raise exceptions.BadRequestKeyError(key)
werkzeug.exceptions.BadRequestKeyError: 400 Bad Request: The browser (or proxy) sent a request that this server could not understand.

Wow thanks!
As it turns out the answer is to not ask a question on this forum and instead spend the weekend learning Selenium as an alternative. Thanks stackoverflow! Thanks Robobrowser!

Related

Bot ignores command, no error message - Discord.py Rewrite

I am pretty new to Python, and I am coding a discord bot using discord.py rewrite, python 3.7. I typed up a command, but the bot seems to completely ignore it, and it doesn't give me any errors.
#client.command(pass_context = True)
async def poll(ctx, polltopic, *pollquestions):
print("Poll command activated.")
reactions = [":one:", ":two:", ":three:", ":four:",":five:",":six:", ":seven:", ":eight:", ":nine:", ":ten:"]
number = 0
await ctx.send("**POLL:** "+str(polltopic))
for x in pollquestions:
await ctx.send(str(reactions[number])+" "+str(x))
number = number+1
The print function used for debugging shows nothing in the output. As other websites advised me to do, I put:
await client.process_commands(message)
at the end of the on_message function. It is still completely ignoring the command, and not giving me any errors.
The problem is probably staring me right in the face, but I don't see it.
I found the error: it had nothing to do with the command syntax itself.
I had a return function exiting the on_message function early, before it got to the await client.process_commands(message), so the bot was ignoring the commands.

FileChooserButton Gtk

Can someone tell me what type of signal for FileChooserButton I have to use, to get file path by clicking on the actual file in FileChooserDialog that was brought up by FileChooserButton? I've used both file_set and file_activated signals but nothing has happened.
I've used an example from Vala language documentation. And I'm not sure whether there should be Open\Cancel buttons in that dialog(See the screenshot below)?
Here is the code, that I've used:
Gtk.FileChooserButton file_chooser = new Gtk.FileChooserButton(
"Select a file",
Gtk.FileChooserAction.OPEN);
file_chooser.set_show_hidden(true);
file_chooser.set_local_only(false);
file_chooser.set_current_folder("/home");
Gtk.FileFilter filter = new Gtk.FileFilter();
filter.add_mime_type("application/x-shellscript");
file_chooser.set_filter(filter);
file_chooser.file_set.connect(() => {
string uri = file_chooser.get_uri();
stdout.printf("Uri: %s", uri);
});
Screenshot
Thanks in advance!
Solved it! The problem was in missing buttons Ok\Cancel, this occurs only in Elementary OS, I believe.
Command:
gsettings set org.gnome.settings-daemon.plugins.xsettings overrides "{'Gtk/DialogsUseHeader':<0>}" did it's job!
Thanks for the replies.

Executing Python Code in IPython Kernel

I want to duplicate ipython notebook capability in Emacs / Pymacs; and I need some direction for a simple code that can 1) send python / "magics" code to a ipython kernel 2) receive the display output, as a string. I found this comment by minrk, the "ipython kernel" example did not work, it gave "ImportError: No module named zmq.blockingkernelmanager".
I had better luck with one his other pointers, finally I landed at ipython-1.1.0/IPython/kernel/inprocess/tests/test_kernel.py, I ripped out a minimal part, and coded an Emacs extension called pytexipy-notebook. It's on Github
goo.gl/kQzJW1
If anyone knows of better examples, such as connecting to an existing (out of process), I'd like to hear about these.
Thanks in advance,
Here is a sample for ipython 3.0.
from IPython.testing.globalipapp import get_ipython
from IPython.utils.io import capture_output
ip = get_ipython()
def run_cell(cmd):
with capture_output() as io:
res = ip.run_cell(content)
print 'suc', res.success
print 'res', res.result
res_out = io.stdout
print 'res out', res_out
content = "print (111+222)"
run_cell(content)
content = "alsdkjflajksf"
run_cell(content)
I will soon update
https://github.com/burakbayramli/emacs-ipython

Using JSON-RPC call from Pyjamas/PyJs with a web.py backend

This has already cost me many hours of Googling and I still cannot get it working, time to ask SO for help :-)
I try to put together a simple test application where the frontend is written in Pyjamas, the backend is running on Web.py. They are supposed to talk to each other via JSON-RPC. The desired functionality is to let the user enter a string which is then converted to uppercase.
There is a description in the Pyjamas online book "Rest of the World" on how to use JSON-RPC which presents various technologies mixed together and is therefore difficult to parse. By looking at hints e.g. from Amund Tveit's blog etc. I cobbled together the following:
1) The server script which is really simple:
import web
import json
import os
urls = (
'/json', 'JSONHandler',
'/json/', 'JSONHandler',
)
app = web.application(urls, globals())
class JSONHandler:
def json_upper(self,args):
return [args[0].upper()]
def json_markdown(self,args):
return [args[0].lower()]
def POST(self):
args = json.loads(web.data())
print args
json_func = getattr(self, 'json_%s' % args[u"method"])
json_params = args[u"params"]
json_method_id = args[u"id"]
result = json_func(json_params)
# reuse args to send result back
args.pop(u"method")
args["result"] = result[0]
args["error"] = None # IMPORTANT!!
web.header("Content-Type","text/html; charset=utf-8")
return json.dumps(args)
if __name__ == "__main__":
app.run()
and it definitely works, tested with a simple query script (not shown) that relies on Josh Marshall's JSON-RPC library.
2) The client-script for Pyjamas which is also straightforward:
# Client example from Amund Tveit's blog
# http://amundblog.blogspot.co.at/2008/12/ajax-with-python-combining-pyjs-and.html
# note: ui and JSONService were not prefixed with pyjamas, but that's needed
from pyjamas.ui import RootPanel, TextArea, Label, Button, HTML, VerticalPanel, HorizontalPanel, ListBox
from pyjamas.JSONService import JSONProxy
class Client:
def onModuleLoad(self):
self.TEXT_WAITING = "Waiting for response..."
self.TEXT_ERROR = "Server Error"
# This is the remote service
self.remote_server = UpperService()
self.status=Label()
self.text_area = TextArea()
self.text_area.setText(r"Please uppercase this string")
self.text_area.setCharacterWidth(80)
self.text_area.setVisibleLines(8)
self.button_py = Button("Send to Python Service", self)
buttons = HorizontalPanel()
buttons.add(self.button_py)
buttons.setSpacing(8)
info = r'Upper-case a string using JSON-RPC'
panel = VerticalPanel()
panel.add(HTML(info))
panel.add(self.text_area)
panel.add(buttons)
panel.add(self.status)
RootPanel().add(panel)
def onClick(self, sender):
self.status.setText(self.TEXT_WAITING)
text = self.text_area.getText()
# (data, response_class): if the latter is 'self', then
# the response is handled by the self.onRemoteResponse() method
if self.remote_server.upper(self.text_area.getText(), self) < 0:
self.status.setText(self.TEXT_ERROR)
def onRemoteResponse(self, response, request_info):
self.status.setText(response)
def onRemoteError(self, code, message, request_info):
self.status.setText("Server Error or Invalid Response: ERROR " + code + " - " + message)
# AJAX calls must come from the same server, only the path is given here
class UpperService(JSONProxy):
def __init__(self):
JSONProxy.__init__(self, "/json/", ["upper"])
I compiled it with PyJs, renamed the default output directory to static so that web.py can serve it, edited static/Client.html so that the internal references point to static:
<html>
<!-- auto-generated html - You should consider editing and adapting this
to suit your requirements. No doctype used here to force quirks mode; see
wiki for details: http://pyjs.org/wiki/csshellandhowtodealwithit/
-->
<head>
<title>Client (Pyjamas Auto-Generated HTML file)</title>
<meta name="pygwt:module" content="/static/Client"> <!-- was content="Client" -->
</head>
<body style="background-color:white">
<script type="text/javascript" src="/static/bootstrap.js"></script> <!-- was src="bootstrap.js" -->
<iframe id="__pygwt_historyFrame" style="display:none;"></iframe>
</body>
</html>
... and then pointing the browser to http://localhost:8080/static/Client.html. All I get is a blank page, inspecting the page source shows static/Client.html above so it was indeed served to the browser. The server's log also shows that at least some pages have been served:
http://0.0.0.0:8080/
127.0.0.1:61466 - - [14/Mar/2013 13:59:39] "HTTP/1.1 GET /static/Client.html" - 200
127.0.0.1:61466 - - [14/Mar/2013 13:59:40] "HTTP/1.1 GET /static/Client.nocache.html" - 200
127.0.0.1:61466 - - [14/Mar/2013 13:59:40] "HTTP/1.1 GET /static/Client.safari.cache.html" - 200
No indication of what went wrong, however. Tried all sorts of other combinations of URLs, renaming directories, compiling the Pyjamas part with the -d option in the hope to get a debug stack trace ... to no avail.
Has anyone succeeded in getting Pyjamas and Web.py working together? If yes, then please share how. Thanks.
PS: I am using web.py V0.37 and the latest Pyjamas development release. (The current stable release V0.8.1 does not work either.)
I know you're probably over this, but finding your code helped me fixing mine and it seems tricky to find working examples online for pyjs with webpy.
Your problem was on the client script side, where you needed to add some code to start the client:
if __name__ == '__main__':
app = Client()
app.onModuleLoad()
Moreover, to avoid errors that will then appear, your first line of import should be changed to:
from pyjamas.ui.RootPanel import RootPanel
from pyjamas.ui.TextArea import TextArea
from pyjamas.ui.Label import Label
from pyjamas.ui.Button import Button
from pyjamas.ui.HTML import HTML
from pyjamas.ui.VerticalPanel import VerticalPanel
from pyjamas.ui.HorizontalPanel import HorizontalPanel
from pyjamas.ui.ListBox import ListBox

making a GET request to a webservice from the playframework 2.0

I'm trying to call a webservice from the play framework, and I think I'm doing it wrong. I have an example call to http://www.myweather2.com/developer/forecast.ashx?uac=eDKGlpcBQN&query=52.6%2C-4.4&output=xml
A snippet from what I'm trying from the playframework is the following:
val response = WS.url("http://www.myweather2.com/developer/forecast.ashx?uac=eDKGlpcBQN&query=52.6%2C-4.4&output=xml").get.get()
val body = response.getBody
When I call this, the body consists of "useraccount does not exist". When I just put this url in a browser, I get the response I'm looking for. What am I doing wrong here?
For some reason, I was getting WS from the wrong import. When I fixed the imports to import play.api.libs.ws.WS, it worked. I'm still amazed it worked halfway with the wrong import
Don't know about "useraccount does not exist" but this seems to work:
val promise = WS.url("http://www.myweather2.com/developer/forecast.ashx?uac=eDKGlpcBQN&query=52.6%2C-4.4&output=xml").get()
val body = promise.value.get.body
Edit: Removed the space.
Also make sure your editor is not inserting a \n or \r after ?
I know this is old, but I just solved this problem while trying to do the same thing - getting the same results.
GET variables must be passed with WS.url("http://...").setQueryParameter(key, value)
Example:
val promise = WS.url("http://www.myweather2.com/developer/forecast.ashx").setQueryParameter("uac", "eDKGlpcBQN").setQueryParameter("query", "52.6%2C-4.4").setQueryParameter("output", "xml").get()
Annoying, but a relatively simple fix.