京东获得JD商品详情 API 返回值说明
item_get-获得JD商品详情 [[查看演示] [API测试] [注册开通]
jd.item_get
| 名称 | 类型 | 必须 | 描述 |
|---|---|---|---|
| key | String | 是 | 调用key(必须以GET方式拼接在URL中) |
| secret | String | 是 | 调用密钥 |
| api_name | String | 是 | API接口名称(包括在请求地址中)[item_search,item_get,item_search_shop等] |
| cache | String | 否 | [yes,no]默认yes,将调用缓存的数据,速度比较快 |
| result_type | String | 否 | [json,jsonu,xml,serialize,var_export]返回数据格式,默认为json,jsonu输出的内容中文可以直接阅读 |
| lang | String | 否 | [cn,en,ru]翻译语言,默认cn简体中文 |
| version | String | 否 | API版本 |
请求参数:num_iid=10335871600
参数说明:num_iid:JD商品ID
Version: Date:
| 名称 | 类型 | 必须 | 示例值 | 描述 |
|---|---|---|---|---|
| item | item[] | 0 | 获得JD商品详情 |
这段Python代码旨在从京东网站上获取商品信息,包括评论数量和评论的关键词,以便进行进一步的分析。该程序分析并模拟了京东的JavaScript请求,以获取动态加载的评论数据。
代码都测试验证过都能正常跑通,实现效果,由于各大网站防爬机制随时可能更新,代码可能失效。思路可以参考
代码主要分为四个部分:
get_product_ids(url): 这个函数通过提供的URL访问京东网站的搜索页,然后从HTML中提取所有商品的ID。
get_comment_summary(product_id): 这个函数接受一个商品ID作为参数,然后向京东的评论概要API发送请求,获取并返回该商品的评论摘要信息,其中包括评论的总数。
get_comment_tags(product_id): 这个函数也接受一个商品ID作为参数,然后向京东的评论标签API发送请求,获取并返回该商品的评论标签,即评论的关键词。
main(url): 这是主函数,它首先从提供的URL获取所有商品的ID,然后对每个商品ID调用上述两个函数获取评论摘要和评论标签,最后将获取的所有数据保存到一个Excel文件中。
import requestsimport jsonimport pandas as pdfrom bs4 import BeautifulSoup# Xpanx.com 专业网络爬虫程序定制,加微信 LiteMango(付费)def get_product_ids(url): headers = {"User-Agent": "Mozilla/5.0"} r = requests.get(url, headers=headers) soup = BeautifulSoup(r.text, "html.parser") product_ids = [item['data-sku'] for item in soup.find_all('li', class_='gl-item')] return product_ids
def get_comment_summary(product_id): comment_summary_url = f'https://club.jd.com/comment/productCommentSummaries.action?referenceIds={product_id}' headers = {"User-Agent": "Mozilla/5.0"} r = requests.get(comment_summary_url, headers=headers) comment_summary = r.json()['CommentsCount'][0] return comment_summary
def get_comment_tags(product_id): comment_tags_url = f'https://club.jd.com/comment/productPageComments.action?productId={product_id}&score=0&sortType=5&page=0&pageSize=10&isShadowSku=0&fold=1' headers = {"User-Agent": "Mozilla/5.0"} r = requests.get(comment_tags_url, headers=headers) comment_tags = r.json()['hotCommentTagStatistics'] return comment_tags
def main(url): product_ids = get_product_ids(url) data = [] for product_id in product_ids: comment_summary = get_comment_summary(product_id) comment_tags = get_comment_tags(product_id) data.append([product_id, comment_summary, comment_tags]) df = pd.DataFrame(data, columns=['Product ID', 'Comment Summary', 'Comment Tags']) df.to_excel('jd_comments.xlsx', index=False)
if __name__ == "__main__": url = "https://search.jd.com/Search?keyword=%E6%89%8B%E6%9C%BA&enc=utf-8&wq=%E6%89%8B%E6%9C%BA&pvid=541bde5713154895ad650c43c4167c10" main(url)