Skip to content

Instantly share code, notes, and snippets.

@tbbooher
Created March 25, 2023 15:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tbbooher/ce3848e40b825c2b7ba3cc624126609b to your computer and use it in GitHub Desktop.
Save tbbooher/ce3848e40b825c2b7ba3cc624126609b to your computer and use it in GitHub Desktop.
from bs4 import BeautifulSoup
import csv
import re
with open('output.html', 'r') as f:
html = f.read()
soup = BeautifulSoup(html, 'html.parser')
results = []
for card in soup.find_all('g-inner-card'):
# print the contents of the h4 tag in the div
# Locate the title
title = card.find('h4', {'role': 'heading'}).get_text(strip=True)
# Locate the price
price = card.find('span', string=lambda x: x and x.startswith('$')).get_text(strip=True)
# Locate the condition
condition = card.find("div", string=re.compile("\d+k (mi|miles)")).get_text(strip=True)
result = f"{title}, {price}, {condition}"
results.append((title, price, condition))
for result in results:
print(result)
# create a csv of the results
with open('output_google.csv', 'w', newline='') as f:
writer = csv.writer(f)
writer.writerow(['Title', 'Price', 'Condition'])
writer.writerows(results)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment