Converting the Solution Into a Script
00:00 So I basically solved the task already in the REPL. So I just need to go ahead and pick out the information that I need from my REPL log, so to say, and write it in the script so that I, that we can run it.
00:13
I want to import mechanicalsoup
00:17
and then create a StatefulBrowser
object,
00:22
which then I need the log in URL. So this is all getting access to the HTML for now log in URL and then browsing there by calling .open()
on it and passing it to URL.
00:35
Cool. Next I need to find the form, which is surprisingly straightforward in this, right? I just need to say browser.select_form()
00:48
and then I can fill the form by using square bracket notation, browser[
, the name of the input field, and then the value that I want to assign to it.
00:59
And finally submit the selected input fields by calling .submit_selected()
and then the browser object navigates to the new page after submitting it.
01:13
So then you’re on the All Profiles page and then you essentially just want to print out the title and that should be it. Exit here and look at our short script, save it, and then go ahead and run it python fill_form
.
01:35
And I don’t get any output because it didn’t print it. So I will wrap browser.page.title
into a call to print()
save again and run it a second time.
01:46
And you can see that we get the requested output. So I start off at the login page, I select the form element, fill the form with the correct information, submit it, the StatefulBrowser
object navigates to the next page.
02:02
And then here I just access its HTML, use the magic that BeautifulSoup
provides to access the title element.
02:11 All right, that’s that. Let me clean that up to show you how little code was necessary to get to this solution.
02:20 Structure it a bit open, select, submit, and here we are. Great. I think that this is very intuitive and quite beautiful code that’s, as you can see, like it’s somewhat written for a scripting.
02:33
Like I don’t need to assign variables even because the StatefulBrowser
object keeps track of changes to itself internally, basically. Of course, you can also go lower level, so there’s another element that MechanicalSoup
provides that’s called browser
.
02:49 This has a little less abstraction on top of it. You can still do everything that you did here with it. If you use that one, that’s totally fine as well.
02:56 That’s an alternative solution. And in some cases, it might be more helpful to have that lower level access. But for something like this site, this is enough and looks great.
Become a Member to join the conversation.