使用爬虫获取 1688 商品详情:高效的数据收罗方法

[复制链接]
发表于 2025-9-20 04:25:08 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
在当今竞争剧烈的电商市场中,获取精准的商品数据对于商家来说至关紧张。无论是举行市场调研、优化产物列表,还是监控监控竞争对手,1688 平台上的商品详情都是名贵的信息资源。本文将介绍如何使用 Python 爬虫技能高效地获取 1688 商品的具体信息,包罗商品名称、代价、图片、形貌等,资助你更好地把握市场动态,优化运营策略。
一、为什么须要爬取 1688 商品详情?

1688 作为国内领先的 B2B 电商平台,拥有海量的商品资源和丰富的供应商信息。通过爬取 1688 商品详情,你可以:


  • 市场调研:相识特定产物的市场趋势和竞争态势。
  • 选品上架:快速找到有潜力的商品,优化你的店肆选品策略。
  • 代价监控监控:及时把握竞争对手的代价动态,调解自己的订价策略。
  • 库存管理:获取商品的库存信息,优化库存管理。
二、准备工作

(一)安装须要的 Python 库

为了实现爬虫功能,我们须要安装以下 Python 库:


  • requests:用于发送 HTTP 哀求。
  • BeautifulSoup:用于剖析 HTML 内容。
  • Selenium:用于处理处罚动态加载的内容。
可以通过以下下令安装这些库:
bash
  1. pip install requests beautifulsoup4 selenium
复制代码
(二)下载 ChromeDriver

为了使用 Selenium,须要下载与欣赏器版本匹配的 ChromeDriver,并确保其路径精确设置。可以从 ChromeDriver 下载页面 获取。
三、编写爬虫代码

(一)发送 HTTP 哀求

使用 requests 库发送 GET 哀求,获取商品页面的 HTML 内容。
Python
  1. import requests
  2. def get_html(url):
  3.     headers = {
  4.         'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'
  5.     }
  6.     response = requests.get(url, headers=headers)
  7.     if response.status_code == 200:
  8.         return response.text
  9.     else:
  10.         print("Failed to retrieve the page")
  11.         return None
复制代码
(二)剖析 HTML 内容

使用 BeautifulSoup 剖析 HTML 内容,提取商品详情。
Python
  1. from bs4 import BeautifulSoup
  2. def parse_html(html):
  3.     soup = BeautifulSoup(html, 'html.parser')
  4.     product_info = {}
  5.     # 提取商品名称
  6.     product_name = soup.find('h1', class_='product-title').text.strip()
  7.     product_info['product_name'] = product_name
  8.     # 提取商品价格
  9.     product_price = soup.find('span', class_='price').text.strip()
  10.     product_info['product_price'] = product_price
  11.     # 提取商品描述
  12.     product_description = soup.find('div', class_='product-description').text.strip()
  13.     product_info['product_description'] = product_description
  14.     # 提取商品图片
  15.     product_image = soup.find('img', class_='main-image')['src']
  16.     product_info['product_image'] = product_image
  17.     return product_info
复制代码
(三)处理处罚动态加载的内容

如果商品详情页的内容是动态加载的,可以使用 Selenium 获取完备的页面内容。
Python
  1. from selenium import webdriver
  2. import time
  3. def get_html_dynamic(url):
  4.     options = webdriver.ChromeOptions()
  5.     options.add_argument('--headless')  # 无头模式
  6.     driver = webdriver.Chrome(options=options)
  7.     driver.get(url)
  8.    
  9.     # 等待页面加载完成
  10.     time.sleep(3)
  11.    
  12.     html = driver.page_source
  13.     driver.quit()
  14.     return html
复制代码
(四)完备示例代码

联合动态加载的内容,完备的示例代码如下:
Python
  1. import requests
  2. from bs4 import BeautifulSoup
  3. from selenium import webdriver
  4. import time
  5. def get_html(url):
  6.     headers = {
  7.         'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'
  8.     }
  9.     response = requests.get(url, headers=headers)
  10.     if response.status_code == 200:
  11.         return response.text
  12.     else:
  13.         print("Failed to retrieve the page")
  14.         return None
  15. def get_html_dynamic(url):
  16.     options = webdriver.ChromeOptions()
  17.     options.add_argument('--headless')  # 无头模式
  18.     driver = webdriver.Chrome(options=options)
  19.     driver.get(url)
  20.    
  21.     # 等待页面加载完成
  22.     time.sleep(3)
  23.    
  24.     html = driver.page_source
  25.     driver.quit()
  26.     return html
  27. def parse_html(html):
  28.     soup = BeautifulSoup(html, 'html.parser')
  29.     product_info = {}
  30.     # 提取商品名称
  31.     product_name = soup.find('h1', class_='product-title').text.strip()
  32.     product_info['product_name'] = product_name
  33.     # 提取商品价格
  34.     product_price = soup.find('span', class_='price').text.strip()
  35.     product_info['product_price'] = product_price
  36.     # 提取商品描述
  37.     product_description = soup.find('div', class_='product-description').text.strip()
  38.     product_info['product_description'] = product_description
  39.     # 提取商品图片
  40.     product_image = soup.find('img', class_='main-image')['src']
  41.     product_info['product_image'] = product_image
  42.     return product_info
  43. def main():
  44.     url = "https://detail.1688.com/offer/123456789.html"
  45.     html = get_html_dynamic(url)  # 使用动态加载的方式获取页面内容
  46.     if html:
  47.         product_info = parse_html(html)
  48.         print("商品名称:", product_info['product_name'])
  49.         print("商品价格:", product_info['product_price'])
  50.         print("商品描述:", product_info['product_description'])
  51.         print("商品图片:", product_info['product_image'])
  52. if __name__ == "__main__":
  53.     main()
复制代码
四、留意事项和发起

(一)遵守网站规则

在爬取数据时,务必遵守 1688 的 robots.txt 文件规定和使用条款,不要频仍发送哀求,以免对网站造成负担或被封禁。
(二)处理处罚非常情况

在编写爬虫步伐时,要思量到大概出现的非常情况,如哀求失败、页面结构变化等。可以通过捕获非常和设置重试机制来进步步伐的稳固性。
(三)数据存储

获取到的商品信息可以存储到文件或数据库中,以便后续分析和使用。
(四)公道设置哀求频率

制止高频率哀求,公道设置哀求隔断时间,例如每次哀求隔断几秒到几十秒,以低落被封禁的风险。
五、总结

通过上述步骤和示例代码,你可以轻松地使用 Python 爬虫获取 1688 商品的具体信息。无论是用于数据分析、市场调研还是用户体验优化,这些数据都将为你提供强盛的支持。渴望本文能资助你快速搭建高效的爬虫步伐,提升你的电商运营服从。
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表