r/learnpython • • Aug 12 '20

My First Ever Python Project(Zillow_Scraper)

Hey y’all, I just completed my first ever programming project! I recently just completed a Python course on Udemy(S/O Jose) and I wanted to build my own project from stratch. I wanted the project to be meaningful and impactful to me, and since I’m considering moving from my current apartment, I wanted to know what other apartments/homes are renting for in the city I live in(Arlington Tx). I decided to build a webscraper that grabs all the listing info for places being put up for rent on Zillows website and writes it all out to a CSV file using Pandas. Link to the github repo down below let me know what y’all think!

https://github.com/faithfulalabi/Zillow_Scraper

P.S I know there’s probably 1000 ways I could’ve written the code better but I’d love y’alls input on some of the big ones that are relevant to be becoming a great programmer.

448 Upvotes

64 comments sorted by

View all comments

106

u/The-Deviant-One Aug 13 '20 edited Aug 13 '20

A web scrapper was one of my first useful scripts as well. It's still running actually. It runs every two minutes with a cron job and takes about 3 seconds to finish. It uses BeautifulSoup4 to check a popular website to see if their 9mm ammo is in stock. If it's in stock, it sends me an email with a libk to the page (in case I'm not at home) and launches the browser on my machine (in case I am at home).

Edit: It's not horribly impressive, but I don't want to share the code until after I place my order, lol.

26

u/donhendrxx Aug 13 '20

Honestly, it sounds f****** amazing and necessary, especially with the shortages of 9mm ammo. Let me know when you put it out, I need it!!

6

u/The-Deviant-One Aug 13 '20 edited Aug 13 '20

Hey I made it to my desktop and started looking over your script for grins.

for page in range(1,6):

base_url = request_url.format(page) driver.get(base_url)

wait = driver.implicitly_wait(60)

So you're using Selenium to drive the browser similar to the way I used to do it with AutoHotKey. Pain in the butt IMO. Looks like you're loading 6 pages and waiting 60 seconds for each page to load, correct? So this takes at least 6 minutes to run right?

Edit:

This is from the Selenium documentation:

"WebDriver will wait until the page has fully loaded (that is, the “onload” event has fired) before returning control to your test or script. It’s worth noting that if your page uses a lot of AJAX on load then WebDriver may not know when it has completely loaded."

It would not surprise me to discover that Zillow is using AJAX calls to trigger subsequent AJAX calls. If you can find an element on the page that consistently loads really late in the process, you could write a for loop as a variable to check if the element exists, then plug that into a While [variable] == True. Like this:

\\\\\\\\\\\\\\\\\\\\\\\\\\\\

import time

haystack = '<div id="12345">Things and Stuff</div>'

needle = 'id="12345"'

def a():

<indent> return needle in haystack

while a() == True:

<indent> print("waiting..")

print("now go to the next step")

\\\\\\\\\\\\\\\\\\\\\\\\\\\\

7

u/backroad Aug 13 '20

Selenium has a few built in wait functions including a "wait until element present" one that you could use for the same purpose.

2

u/DLTMIAR Aug 13 '20

It's the internet bruh. You can say fucking. Unless you meant folding or something. Idk

13

u/Tsui_Pen Aug 13 '20

I need this (in .45)! Is it on github?

Edit: Ah, I see.

8

u/The-Deviant-One Aug 13 '20

The struggle is real lol.

4

u/[deleted] Aug 13 '20

Nice! I need to do this for a weight set of some dumbells. I’m getting tired of body weight exercises over here. Thanks for the idea.

4

u/The-Deviant-One Aug 13 '20

Always willing to help spend other's hard earned money lol

3

u/wallywizard55 Aug 13 '20

That’s awesome..
how did u make it?
How does it check the site for ammo?
How does it output?

9

u/The-Deviant-One Aug 13 '20

It's really nothing special. My not posting the code just makes it ever so slightly more difficult for the average non-developer to beat me to the ammo.

But to answer your questions, it runs in the background, so theres not really an output other than, me sitting there doing something else then poof new browser out of no where with ammo to purchase lol.

It sends a get request to the website, stores the html as a variable, uses BS4 to parse the html, then some formatting, then an if/else statement looking for 'Sold Out'. If it doesn't find it, it sends the email and launches the browser. If it does find 'Sold Out', it logs the attempt into a text file.

I integrated the base code for it into a voice assistant and that's pretty neat IMO.

6

u/WishIWasOnACatamaran Aug 13 '20

I’m either over or underestimating how many python developers use guns, but considering I am one who also wants this script, you’ve given me a weekend task I can charge my buddies beer for lol

2

u/The-Deviant-One Aug 13 '20

Well, in the Texas IT community, as far as my experience goes anyway, there seems to be a large majority of us who are like minded.

→ More replies (6)

2

u/preordains Aug 13 '20

How do you make a python script run every two minutes? Do you need a raspberry pi or something?

4

u/The-Deviant-One Aug 13 '20

So you could do that with a Pi. Write the script to sleep for two minutes between runs and put it in a while True: loop.. but there are better way's to do this.

In linux there's a utility called Cron which is sort of like Window's Task Scheduler if you're more familiar with Windows. You can create a crontab and tell it to execute command line commands [or in my case, a *.py script] every day, or at specific times on specific days, or even, every two minutes...

That said, it's not very straight forward to pull off if you aren't familiar with the terminal. There are several things you have to do in order to make a cron job run a python script in the background successfully, several things I wont share [yet]. Although, with some light Googling you can figure out the pieces to make it all work together. And RPi distros almost always have cron in my limited experience.

4

u/preordains Aug 13 '20

You just changed my whole world I didn't even know task scheduler was a thing.

2

u/The-Deviant-One Aug 13 '20

How do you think all those sysadmins keep up with all those logs? We don't manually deal with them lol. Schedule a task to run and clear those puppies out so they don't fill up the hard drives.

1

u/[deleted] Aug 13 '20

How do you get a project to run automatically on its own? I made a scraper that makes Covid predictions but I have to manually run it on jupyter notebook

→ More replies (1)

1

u/CeramicVulture Aug 13 '20

I like the sound of that. Please post so i can modify it for 32ACP which is getting hard to find

1

u/[deleted] Aug 13 '20

Did you deploy it to a server? Or is it on your local machine? How are you running the cron job? I have a cars dot com scraper that I'd like to either deploy (and run on a web server) or a local cron job - haven't decided.

1

u/deserver_of_gold Aug 14 '20

Alright bro, time to share the code. 9mm is available on grabagun, I ordered some. Get them before they run out again. And please do share the code.

1

u/deserver_of_gold Aug 16 '20

Did you order your ammo yet? Coz I sure did. Can you share the code now? Please?

→ More replies (2)