A Proven Framework for Exporting Your Medium Followers
Medium is undergoing lots of changes, it is really evolving nowadays. I love to write on the sleek interface and I love to tweak Medium. Maybe more than writing on it.
So one of my friends had a problem that was a gold one: She wanted to export her follower list from Medium to CSV. By hand this would have taken her ages for about 2000 followers.
I haven’t yet inspected the followers page (actually I didn’t even know Medium has an option to see who is following you) so this was a completely new field for me. Fortunately she knows about HTML and she could easily find out that elements have the same classes. This is a point that I could use so I started coding the solution in Python right away. It was a good exercise for me because I am learning Python and I learned like two times more from this task than from like the first 100 pages of the book I read and from the videos I have seen.
Yes, I will give you the code in a second. Just let me give you some instructions. First of all, you need to scroll down to the end of your follower list by clicking on the ‘Show More’ lots of times (it depends on how many followers you have). Second, you need to save the page after you scrolled down. It is the easiest if you right-click and click ‘Save as..’ or something like that. If you can’t find it that way, you can just hit Ctrl-S or Cmd-S in case you use Mac. Anyway, try to save it into a separate folder. Now you are done with about half of the job. Congrats!
After saving the files you will see a file and a folder. If you have chosen ‘index.html’ as the name of the file, open that file in a text editor. It is important because if you just double-click it, it will open in your browser (unless you configured it to open somewhere else but in this case I guess you are a programmer and you already know what is going on). Copy the contents of the file. The easiest way is Ctrl-A and then Ctrl-C. In Mac, change Ctrl to Cmd.
Here comes the tricky part! I coded this thing in Python and it is hard for an average user to set up a Python development environment. Fortunately it is dead simple (Halloween intended) by using a free cloud editor called Cloud9.
Just sign up and create a new box with this URL: https://c9.io/new. It is very important to select Django as a template so you already have Python shipped.
After you created your your amazing virtual machine you will see a code editor popping up. COOL!
Over here create a file named ‘main.py’ by right-clicking the folder on the left that has the same name as your project name when you created it. (In my case it is ‘your-fancy-exporter-name’ from the image above.
Right now comes the super fun part! Paste this code into
import bs4 import re txt = open("index.html") def read1k(): return txt.read(65443553) for data in iter(read1k, ''): soup = bs4.BeautifulSoup(data, "html5lib") links = [a.attrs.get('href') for a in soup.select('div.list-itemDescription > a')]; names = [a for a in soup.select('div.list-itemDescription > a')]; descriptions = [p for p in soup.select('p.list-itemDescription')]; i = 0 length = len(descriptions) while(i < len(links)): ez = links[i] + "\t" + re.sub(' +',' ',names[i].text).replace('\n', '\t').replace('\r', '').encode('ascii', 'ignore').decode('ascii') + "\n" with open("output.csv", "a") as myfile: myfile.write(ez) i+= 1 print("The function finished with %d found followers. You can download 'output.csv' now")%(len(links))
At this point if you could follow, I must say you rock!
So to the next step: Boring installation of dependencies.
Paste this command in the terminal in the bottom of the page:
sudo pip install beautifulsoup4
Right now just before running the code, you need to insert your Medium page HTML content into a new file called ‘index.html’ the same way as you created main.py. The problem with this is that it is unformatted and the script has problems selecting data from it in it’s current stage. So right now You must format it by clicking on Edit > Code Formatting > HTML while your index.html file is open in the editor.
And Here Comes the Magic!
into the terminal and right now you have an ‘output.csv’ in your file tree on the left! Hooray! That’s your CSV data! WOW, you made it this far! You can now just right-click to that CSV and download it from the drop-down menu. That’s it!
This was a featured article on Medium, this is why I showed you this on Codementor too, in case you would like to see my code or use it!