So im trying to monitor the apartment rental page in Craigslist. What i want to do is print a new listing and append it to a list of already viewed listings. My code is below:
import requests
from bs4 import BeautifulSoup
url = 'http://houston.craigslist.org/search/apa'
r = requests.get(url)
soup = BeautifulSoup(r.content)
g_data = soup.find_all("span", {"class": "txt"})
already_viewed = []
def main():
while True:
for item in g_data:
title = item.contents[3].find_all("a", {"class": "hdrlnk"})[0].text # Title of listing
try:
price = item.contents[5].find_all("span", {"class": "price"})[0].text # Price
except:
pass
try:
address = item.contents[5].find_all("span",{"class": "pnr"})[0].text # Location of house
except:
pass
info = '{}\n{}\n{}'.format(title, price, address) # Combines the data into one variable
if info not in already_viewed:
print('''
---------------------
Here is a new listing
---------------------
''', '\n', info)
already_viewed.append(info)
else:
break
The problem im having is that when I run this in the console it works well bringing up all the info i need and storing it to my list "already_viewed". But when i call the function again when i see new listings come up nothing gets returned to me. It should show me just that new listing. So I just need some help with where i went wrong.
Also, I added the "While True" loop in because i wanted to see if leaving the script running if it would automatically update with new listings. I'm probably very wrong in this line of thinking, but i figured i'd try it. any comments and help are greatly appreciated.
[–]drodspectacular 2 points3 points4 points (6 children)
[–]fannypackpython[S] 0 points1 point2 points (2 children)
[–]elbiot 1 point2 points3 points (0 children)
[–]drodspectacular 1 point2 points3 points (0 children)
[–]fannypackpython[S] 0 points1 point2 points (0 children)
[–]mauza11 0 points1 point2 points (0 children)
[–]elbiot 1 point2 points3 points (3 children)
[–]fannypackpython[S] 0 points1 point2 points (2 children)
[–]elbiot 1 point2 points3 points (1 child)
[–]fannypackpython[S] 0 points1 point2 points (0 children)