Forums

How to set up a bot to post on your own database/post site?

How to auto post on your own basic pythonanywhere blog?

for example I've set up login / post page, basically the Miguel Grinberg style one.

Just as a test, want to automate the procedure.

any ideas?

so just as a basic intro, I want a script that logs into the blog for me, just by pressing a link

I can seed the script with username and password to get going.

ie. post 'hi there or something!' to here:

http://blueforest.pythonanywhere.com/

In general, logging in and adding a post to a blog just involves HTTP POST requests sent to the blog. You can use the Python module called requests to send requests to you blog so you can do things from your script.

tried that but it comes up with

405 Method not allowed

for example this gives that error message:

import requests


url = 'https://madmartin.pythonanywhere.com/base'
values = {'username': 'old',
          'password': 'man33'}

r = requests.post(url, data=values)
print (r.content)

oh, by the way, I am using one of my free accounts (BlueForest)

and no worries about the password, it's just a spare page which anyone can look at

If the place you are doing the requests from is a free account, then it would be restricted to a whitelist- you wouldn't be able to access the blog website.

Also, if it is django etc, maybe you also need the csrf token etc.

I'm actually doing the requests from Idle on laptop to connect with a password protected page on a paid for account

can that work?

so, just for now want to be able to get into page that has pythonanwhere password added to it

If you're using the "Password protection" feature on the web app configuration page, then that's just HTTP Basic Auth. You can use that from requests: https://requests.readthedocs.io/en/master/user/authentication/

the problem I'm having is actually populating the username and password fields.

so, the code I'm now trying is:

import requests
import webbrowser

webbrowser.open('https://madmartin.pythonanywhere.com/base', new=0)
from requests.auth import HTTPBasicAuth
requests.get('https://madmartin.pythonanywhere.com/base', auth=('old', 'man33'))
print('attempting..')

that brings up the page and a PAnywhere pop-up box asking for username and password, which is empty.

So, I'm trying to get auto fill here - ie. username = old , password = man33

meaning I want the script to fill the fields in automatically and thus take me straight into https://madmartin.pythonanywhere/base

can't get this to work

now trying this which also doesn't fill in the text box with anything

import requests
import webbrowser

webbrowser.open('https://madmartin.pythonanywhere.com/form', new=0)

url = 'https://madmartin.pythonanywhere.com/form'
r = requests.post(url, data = {  'text': 'one of the form fields'} )

with open("https://madmartin.pythonanywhere.com/mysite/templates/form", "wb") as code:
    code.write(r.content)

text box is actually called text

The webbrowser instance you have there and the requests session are 2 entirely different things. You cannot drive that webbrowser from requests.

You can use selenium if you want to drive a browser, but I'm not sure how well it deals with basic auth.

The requests session works without a browser. It is just sending requests and receiving responses from the server.

ok, thanks , awesome! I'm making some progress now using Selenium