Reputation: 164
I've written a script in python to scrape the name of diferent items from a webpage. My script can do it errorlessly. Some items appeared more than once. I would like to scrape the number of each item's appearance.
import requests
from bs4 import BeautifulSoup
baseUrl = 'https://www.etsy.com/shop/JpKrHk/sold?ref=pagination&page=2'
res = requests.get(baseUrl)
soup = BeautifulSoup(res.text,'lxml')
items = [item.get_text(strip=True) for item in soup.select(".v2-listing-card__info h2")]
print(len(items))
What is the right way to find out how many times each item has appeared in the list?
Upvotes: 1
Views: 59
Reputation: 2687
In addition to using Counter as suggested in the comments, you could also use a dict comprehension and list
's count()
method:
import requests
from bs4 import BeautifulSoup
baseUrl = 'https://www.etsy.com/shop/JpKrHk/sold?ref=pagination&page=2'
res = requests.get(baseUrl)
soup = BeautifulSoup(res.text,'lxml')
items = [item.get_text(strip=True) for item in soup.select(".v2-listing-card__info h2")]
items = {i:items.count(i) for i in items}
print(items)
>>> {'1612-Plain 08, 3 sheets Korean Cotton Sticker sheet Suatelier Kawaii sticker, Cute stickers, Scrapbooking materialPlanner': 1, '1610-Plain 06, 3 sheets Korean Cotton Sticker sheet Suatelier Kawaii sticker, Cute stickers, Scrapbooking materialPlanner': 1, '1605-Plain 01, 6mm Korean Sticker sheet Suatelier Kawaii sticker, Cute stickers, Scrapbooking material 3 sheetsPlanner': 1, 'best offer 1 Pen with 5 refills pcs Japan [Muji] MomA 0.38mm/0.5mm Gel INK PEN Black/Blue COLOUR': 1, '1061- daily Korean Travel Sticker sheet Suatelier Kawaii sticker, Cute stickers, Scrapbooking materialPlanner': 1, '2018 new collection 60 style can choose One of the Limited Japan Kinds Pilot Frixion Stamp SPF-12 erasable scrapbook': 14, 'Zebra Justfit Mojini Line Highlighter - 5 Color Set WKS22-5C': 1, "Let's Color 6 colors fast dry Ink Pads for Fingerprints, Brilliance Drop Ink Pad, Fingerprint Ink Pad, Thumbprint Guest Book": 1, '2019 new collection One of 98-115 the Limited Japan Kinds Pilot Frixion Stamp SPF-12 erasable': 1, '1082 - daily in Tokyo Korean sheet Suatelier Kawaii sticker, Cute stickers, Scrapbooking materialPlanner': 1, '2018 new collection 72 style can choose One of 61-72 the Limited Japan Kinds Pilot Frixion Stamp SPF-12 erasable scrapbook': 1}
Upvotes: 1