Project 2: Scraping eBay
Due: Wednesday, October 21 at 11:59pm · Worth: 23 points (+1 extra credit; maximum 24/23)
You’ll write ebay-dl.py, a command-line program that collects eBay listings for a search term. It will turn each listing into a Python dictionary and save the results in JSON so you can compare items across pages.
Start
Create an empty GitHub repository named ebay-scraper, clone it, and set up its Python environment. Install the two libraries:
python -m pip install requests beautifulsoup4Put requests and beautifulsoup4 in requirements.txt so someone installing your program knows which libraries it needs.
Create ebay-dl.py and use argparse to accept a search term, as in the command-line example. The commands below use --search:
python ebay-dl.py --search "garden tools"A positional search term, --query, or --term is also accepted by the grader. Choose one interface without subcommands and document it in your README.
Download Pages
Start with one search-results page, then extend your program to download pages 1 through 10. Use requests with the search term in _nkw and the page number in _pgn. This self-contained example downloads the first page:
import requests
response = requests.get(
'https://www.ebay.com/sch/i.html',
params={'_nkw': 'garden tools', '_pgn': 1},
headers={'User-Agent': 'Mozilla/5.0'},
timeout=10,
)
response.raise_for_status()
print(response.text[:200])In your program, use the supplied search term and your loop’s page number. The User-Agent header identifies the requesting software. Include the browser-style value above and a timeout on each request, and pause between pages. If eBay blocks requests or returns a robot check, stop live requests and use the saved practice pages.
Saved Practice Pages
Download practice-pages.zip into your repository and extract it:
python -m zipfile -e practice-pages.zip .The folder contains ten HTML pages with artificial listings. Open practice-pages/page-1.html in your browser and editor to inspect the structure. Download read_practice_pages.py for a runnable example of reading these files:
python read_practice_pages.py --pages-dir practice-pagesWrite one extraction function that accepts HTML as a string. With --pages-dir, have your program read page-1.html through page-10.html from the supplied folder and pass their contents to that function. The file-reading loop in the example shows how. Without the option, pass the HTML from each of the ten downloaded pages to the same function.
Extract Listings
Use Beautiful Soup to extract every listing on each page. Build a list of dictionaries with these six fields:
| Key | Python value |
|---|---|
name |
Item name as a string. |
price |
Integer cents. For a price range, use either endpoint. |
status |
Condition as a string, such as "Brand New" or "Pre-owned". |
shipping |
Integer cents, or 0 for free shipping. |
free_returns |
Boolean: True for free returns, False when returns are explicitly not free. |
items_sold |
Number sold as an integer. |
Include all six keys in every record. Use None when the listing leaves a field out. For example, a listing with no returns information gets None, while one that explicitly rules out free returns gets False. Store money in cents, so $11.95 becomes 1195.
Check your extraction on the practice pages:
- There are 30 listings across the ten pages. Include each once.
- The first trowel costs 1195 cents and ships free.
- The first planter costs 1250 or 1800 cents, with 425-cent shipping.
- Mystery-seed listings have
Nonefor their missing fields. - The final result includes listings from page 10.
Keep support for the practice pages’ HTML structure if you also adapt your scraper to live pages.
Save Results
Use json to write the combined list to SEARCH_TERM.json, using the supplied search term as the filename. For example:
python ebay-dl.py --search "garden tools" --pages-dir practice-pagesThis should create garden tools.json. Python’s None values become JSON null values. Try another search term to check that the request and output filename change. The saved practice pages contain the same listings regardless of the search term.
For a reproducible example, keep one JSON output and a short README explaining your program and the exact command that produced that file. Say whether it contains live listings or artificial practice data. This example is for checking and sharing your work, with no separate grading points.
Grading
The 23 points come from running your program:
-
Use the supplied search term in every request and the output filename.
-
Request pages 1 through 10 using
requests, with a browser-styleUser-Agent. -
Extract the listings and field values with Beautiful Soup.
-
Save valid JSON with the required keys and types.
-
Include listings from every downloaded page.
Gradescope replaces network requests with ten saved HTML pages and runs without --pages-dir. The pages follow the practice structure, with different listing values. No live eBay requests are made during grading.
Extra Credit
-
Add
--csvto save the same listings asSEARCH_TERM.csvinstead of JSON, for a maximum of 24/23.
Include a header with the six field names. Save one example CSV and add its command to your README.
Submit
Commit and sync ebay-dl.py, requirements.txt, your README, and your example output. If claiming CSV extra credit, also include this file at the repository root:
# submission.toml
[features]
csv = trueChoose GitHub on Gradescope and submit your ebay-scraper repository and branch. The CSV test runs only when claimed.
FAQs
Why do some prices end up attached to the wrong listings?
If you collect all titles and all prices separately, one missing price shifts the correspondence between the two lists. Loop through listing cards instead. Find the title, price, and other fields inside each card, then build that listing’s dictionary.
When should a missing field become None, False, or 0?
Use None when the page doesn’t supply the information. Use False when it explicitly says returns aren’t free, and 0 when it explicitly says shipping is free. Those values answer different questions: a missing shipping price doesn’t tell you that shipping costs nothing.
Why can converting a price to cents lose a cent?
A float stores an approximation of a decimal price. Multiplying by 100 and then calling int can truncate a value just below the intended integer. Parse the dollars and cents as text, or use decimal.Decimal directly on the cleaned price string before converting to integer cents.
The request succeeded, but I found no listings. What should I check?
Save and inspect response.text. A successful HTTP response can contain a robot check instead of search results. If that’s what you received, switch to the supplied practice pages. If the HTML contains listings, compare your selectors with the structure of one listing card.