Python RoboBrowser Auto Search Function
What's wrong with this code? It works with duckduckgo.com but not google, wikipedia or yahoo. And yes, I change the ID to match the specific site.
import re
from robobrowser import RoboBrowser
browser = RoboBrowser()
browser.open("https://en.wikipedia.org/wiki/Wikipedia")
# Must find the proper id in the html
form = browser.get_form(id = "searchInput")
form
form["searchval"].value = "Beethoven Opus 131"
browser.submit_form(form)
links = browser.get_links()
for link in links:
print(link)
print("Le Fin.")
I am getting the following error every time (except duckduckgo.com)
line 16, in <module>
form["searchval"].value = "Beethoven Opus 131"
TypeError: 'NoneType' object is not subscriptable
Why am I getting the error NoneType
here? I know the variable hasn't been defined previously, but not with Duckduckgo. Please, help.
source to share
First of all, you are defining incorrectly form
. Element c id="searchInput"
is an element input
, whereas you want an element form
- it has id="searchform"
.
Also, since there are two submit buttons, you need to robobrowser
specify which one to use:
form = browser.get_form(id="searchform")
form["search"].value = "Beethoven Opus 131"
browser.submit_form(form, submit=form.submit_fields['go'])
Worked for me.
source to share