From e8df1517e32250aadfa7a7c922b0f2e5b061d4ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20F=C3=A1w=E1=BB=8Dl=C3=A9?= Date: Tue, 7 Nov 2023 17:44:28 +0100 Subject: [PATCH] Create scrapeEbay.py This is the code for an article I wrote recently. After reading the article, I want people to access the code via a repo easily. Hence, the reason I am raising this PR. --- scrapeEbay.py | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 scrapeEbay.py diff --git a/scrapeEbay.py b/scrapeEbay.py new file mode 100644 index 0000000..9bee35a --- /dev/null +++ b/scrapeEbay.py @@ -0,0 +1,60 @@ +import requests +import json +from bs4 import BeautifulSoup + +API_KEY = "YOUR_API_KEY" +url = "https://www.ebay.com/sch/i.html?_from=R40&_trksid=p4432023.m570.l1312&_nkw=airpods+pro&_sacat=0" + +payload = {"api_key": API_KEY, "url": url} +r = requests.get("http://api.scraperapi.com/", params=payload) +html_response = r.text +soup = BeautifulSoup(html_response, "lxml") + + +result_list = [] + +# Find all product items on the page +listings = soup.find_all("div", class_="s-item__info clearfix") +images = soup.find_all("div", class_="s-item__wrapper clearfix") + +for listing, image_container in zip(listings, images): + title = listing.find("div", class_="s-item__title").text + price = listing.find("span", class_="s-item__price").text + + product_url = listing.find("a") + link = product_url["href"] + + product_status_element = listing.find("div", class_="s-item__subtitle") + product_status = ( + product_status_element.text + if product_status_element is not None + else "No status available" + ) + + if title and price: + title_text = title.strip() + price_text = price.strip() + status = product_status.strip() + + image = image_container.find("img") + image_url = image["src"] + + result_dict = { + "title": title_text, + "price": price_text, + "image_url": image_url, + "status": status, + "link": link, + } + result_list.append(result_dict) + +# print(result_list) + +# Output the result in JSON format +output_json = json.dumps(result_list, indent=2) + +# Write the JSON data to a file +with open("ebay_results.json", "w", encoding="utf-8") as json_file: + json_file.write(output_json) + +print("JSON data has been written to ebay_results.json")