Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Can';t测量列表中每个项目的频率_Python_Python 3.x_Web Scraping - Fatal编程技术网

Python Can';t测量列表中每个项目的频率

Python Can';t测量列表中每个项目的频率,python,python-3.x,web-scraping,Python,Python 3.x,Web Scraping,我已经用python编写了一个脚本来从网页中刮取不同项目的名称。我的脚本可以无误地完成它。有些物品不止出现一次。我想刮掉每个项目的外观编号 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.tex

我已经用python编写了一个脚本来从网页中刮取不同项目的名称。我的脚本可以无误地完成它。有些物品不止出现一次。我想刮掉每个项目的外观编号

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))

找出每个项目在列表中出现多少次的正确方法是什么?

除了使用注释中建议的计数器外,您还可以使用dict理解和
列表
计数()方法:

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}
您可以使用来计算频率<代码>c=计数器(项目);打印(c.most_common())
可能的副本请查看此问题和相关问题。