Search code examples
pythonpython-3.xbeautifulsouppython-requestscraigslist

Beautiful Soup Craigslist Scraping Pricing is the same


I am trying to scrape Craigslist using BeautifulSoup4. All data shows properly EXCEPT price. I can't seem to find the right tagging to loop through pricing instead of showing the same price for each post.

import requests 
from bs4 import BeautifulSoup

source = requests.get('https://washingtondc.craigslist.org/search/nva/sss?query=5%20hp%20boat%20motor&sort=rel').text
soup = BeautifulSoup(source, 'lxml')

for summary in soup.find_all('p', class_='result-info'):
    pricing = soup.find('span', class_='result-price')
    price = pricing
    title = summary.a.text
    url = summary.a['href']
    print(title + '\n' + price.text + '\n' + url + '\n')

Left: HTML code from Craigslist, commented out is irrelevant (in my opinion) code. I want pricing to not loop the same number. Right: Sublime SS of code.

Snippet of code running through terminal. Pricing is the same for each post.

Thank you


Solution

  • Your script is almost correct. You need to change the soup object for the price to summary

    import requests 
    from bs4 import BeautifulSoup
    
    source = requests.get('https://washingtondc.craigslist.org/search/nva/sss?query=5%20hp%20boat%20motor&sort=rel').text
    soup = BeautifulSoup(source, 'lxml')
    
    for summary in soup.find_all('p', class_='result-info'):
        price = summary.find('span', class_='result-price')
        title = summary.a.text
        url = summary.a['href']
        print(title + '\n' + price.text + '\n' + url + '\n')
    

    Output:

    Boat Water Tender - 10 Tri-Hull with Electric Trolling Motor
    $629
    https://washingtondc.craigslist.org/nva/boa/d/haymarket-boat-water-tender-10-tri-hull/7160572264.html
    
    1987 Boston Whaler Montauk 17
    $25450
    https://washingtondc.craigslist.org/nva/boa/d/alexandria-1987-boston-whaler-montauk-17/7163033134.html
    
    1971 Westerly Warwick Sailboat
    $3900
    https://washingtondc.craigslist.org/mld/boa/d/upper-marlboro-1971-westerly-warwick/7170495800.html
    
    Buy or Rent. DC Party Pontoon for Dock Parties or Cruises
    $15000
    https://washingtondc.craigslist.org/doc/boa/d/washington-buy-or-rent-dc-party-pontoon/7157810378.html
    
    West Marine Zodiac Inflatable Boat SB285 With 5HP Gamefisher (Merc)
    $850
    https://annapolis.craigslist.org/boa/d/annapolis-west-marine-zodiac-inflatable/7166031908.html
    
    2012 AB aluminum/hypalon inflatable dinghy/2012 Yamaha 6hp four stroke
    $3400
    https://annapolis.craigslist.org/bpo/d/annapolis-2012-ab-aluminum-hypalon/7157768911.html
    
    RHODES-18’ CENTERBOARD DAYSAILER
    $6500
    https://annapolis.craigslist.org/boa/d/ocean-view-rhodes-18-centerboard/7148322078.html
    
    Mercury Outboard 7.5 HP
    $250
    https://baltimore.craigslist.org/bpo/d/middle-river-mercury-outboard-75-hp/7167399866.html
    
    8 hp yamaha  2 stroke
    $0
    https://baltimore.craigslist.org/bpo/d/8-hp-yamaha-2-stroke/7154103281.html
    
    TRADE 38' BENETEAU IDYLLE 1150
    $35000
    https://baltimore.craigslist.org/boa/d/middle-river-trade-38-beneteau-idylle/7163761741.html
    
    5-hp Top Tank Mercury
    $0
    https://baltimore.craigslist.org/bpo/d/5-hp-top-tank-mercury/7154102434.html
    
    5-hp Top Tank Mercury
    $0
    https://baltimore.craigslist.org/bpo/d/5-hp-top-tank-mercury/7154102744.html
    
    Wanted ur unwanted outboards
    $0
    https://baltimore.craigslist.org/bpo/d/randallstown-wanted-ur-unwanted/7141349142.html
    
    Grumman Sport Boat
    $2250
    https://baltimore.craigslist.org/boa/d/baldwin-grumman-sport-boat/7157186381.html
    
    1996 Carver 355 Aft Cabin Motor Yacht
    $47000
    https://baltimore.craigslist.org/boa/d/middle-river-1996-carver-355-aft-cabin/7156830617.html
    
    Lower unit, long shaft
    $50
    https://baltimore.craigslist.org/bpo/d/catonsville-lower-unit-long-shaft/7155566763.html
    
    Lower unit, long shaft
    $50
    https://baltimore.craigslist.org/bpo/d/catonsville-lower-unit-long-shaft/7155565771.html
    
    Lower unit, long shaft
    $50
    https://baltimore.craigslist.org/bpo/d/catonsville-lower-unit-long-shaft/7155566035.html
    
    Lower unit, long shaft
    $50
    https://baltimore.craigslist.org/bpo/d/catonsville-lower-unit-long-shaft/7155565301.html
    
    Cape Dory 25 Sailboat for sale or trade
    $6500
    https://baltimore.craigslist.org/boa/d/reedville-cape-dory-25-sailboat-for/7149227778.html
    
    West Marine HP-V 350
    $1200
    https://baltimore.craigslist.org/boa/d/pasadena-west-marine-hp-350/7147285666.html