Search code examples
pythonlistweb-scrapingbeautifulsoupcomparison

Comparing results with Beautiful Soup in Python


I've got the following code that filters a particular search on an auction site. I can display the titles of each value & also the len of all returned values:

from bs4 import BeautifulSoup
import requests


url = requests.get("https://www.trademe.co.nz/a/marketplace/music-instruments/instruments/guitar-bass/electric-guitars/search?search_string=prs&condition=used")
soup = BeautifulSoup(url.text, "html.parser")
listings = soup.findAll("div", attrs={"class":"tm-marketplace-search-card__title"})
print(len(listings))

for listing in listings:
    print(listing.text)

This prints out the following:

#print(len(listings))
3 

#for listing in listings:
#    print(listing.text)

 PRS. Ten Top Custom 24, faded Denim, Piezo. 
 PRS SE CUSTOM 22 
 PRS Tremonti SE *With Seymour Duncan Pickups* 

I know what I want to do next, but don't know how to code it. Basically I want to only display new results. I was thinking storing the len of the listings (3 at the moment) as a variable & then comparing that with another GET request (2nd variable) that maybe runs first thing in the morning. Alternatively compare both text values instead of the len. If it doesn't match, then it shows the new listings. Is there a better or different way to do this? Any help appreciated thank you


Solution

  • Fixed it with the following:

    allGuitars = ["",]
    
    latestGuitar = soup.select("#-title")[0].text.strip()
    
     if latestGuitar in allGuitars[0]:
        print("No change. The latest listing is still: " + allGuitars[0])
      elif not latestGuitar in allGuitars[0]:
        print("New listing detected! - " + latestGuitar)
        allGuitars.clear()
        allGuitars.insert(0, latestGuitar)