Created
March 25, 2023 15:05
-
-
Save tbbooher/ce3848e40b825c2b7ba3cc624126609b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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