1

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?

MITHU
  • 113
  • 3
  • 12
  • 41
  • 5
    You can use [`Counter`](https://docs.python.org/3/library/collections.html#collections.Counter) to count frequencies. – zvone Mar 26 '19 at 19:33
  • 1
    i.e. `c = Counter(items); print(c.most_common())` – n1c9 Mar 26 '19 at 19:41
  • Possible duplicate of [How to find duplicate elements in array using for loop in Python?](https://stackoverflow.com/questions/1920145/how-to-find-duplicate-elements-in-array-using-for-loop-in-python) – aparpara Mar 26 '19 at 19:55
  • Please look at this https://stackoverflow.com/questions/1920145/how-to-find-duplicate-elements-in-array-using-for-loop-in-python and related questions. – aparpara Mar 26 '19 at 19:55

1 Answers1

1

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}
n1c9
  • 2,662
  • 3
  • 32
  • 52