Vivino.com爬虫筛选英文评论与多国葡萄酒数据问题咨询
Vivino爬取问题解决方案
1. 仅筛选英文评论
Vivino的评论接口返回的单条评论对象自带language字段,值为en即代表英文内容,可直接通过该字段过滤,也可搭配语言检测库做二次校验提升准确率。
代码修改点:
找到原代码中遍历评论的循环,增加过滤条件即可:
# 可额外安装langdetect做二次校验,不需要可省略 # 安装命令:pip install langdetect from langdetect import detect, LangDetectException for r in d["reviews"]: # 过滤条件:1. 评论语言标记为英文 2. 评论内容不为空 if r.get("language") == "en" and r.get("note"): # 可选:二次校验内容是否为英文 try: if detect(r["note"]) != "en": continue except LangDetectException: # 内容过短无法检测直接跳过 continue ratings.append( [ row["Year"], row["Wine ID"], r["rating"], r["note"], r["created_at"], ] )
不需要二次校验的话直接保留第一个if判断即可。
2. 实现多国家葡萄酒数据爬取
Vivino的探索接口country_codes[]参数为数组类型,删除该参数后接口会缺少筛选条件报错,正确用法是传入国家代码列表即可实现多国家筛选,不需要该参数限制国家时直接从params字典中删除该键即可。
代码修改点:
找到原代码中请求探索接口的params部分,修改country_codes[]的值即可:
# 示例:同时爬取葡萄牙pt、法国fr、意大利it三个国家的葡萄酒 r = requests.get( "https://www.vivino.com/api/explore/explore", params={ "country_code": "FR", "country_codes[]": ["pt", "fr", "it"], # 此处改为国家代码列表 "currency_code": "EUR", "grape_filter": "varietal", "min_rating": "1", "order_by": "price", "order": "asc", "page": 1, "price_range_max": "500", "price_range_min": "0", "wine_type_ids[]": "1", }, headers={ "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:66.0) Gecko/20100101 Firefox/66.0" }, )
如果需要爬取所有国家的葡萄酒,直接把"country_codes[]": "pt"这一行从params字典中删除即可,不要留空值。
内容的提问来源于stack exchange,提问作者B Moeskops
相关产品推荐
相关产品推荐

