Python网络爬虫实战:抓取电商商品数据示例

月亮慢半拍
月亮慢半拍
发布时间:2026-10-13 | 3人浏览过
原创

本文介绍如何利用Python编写网络爬虫程序,自动抓取万维网上的电商售卖信息。通过示例代码展示如何使用Requests发起HTTP请求,并结合BeautifulSoup解析HTML页面结构,提取商品标题、价格、发布时间及地区等关键字段。该示例以58同城电脑售卖信息为对象,演示了URL列表获取与详情页数据提取的基本流程,旨在帮助开发者掌握基础的数据爬取技巧。

网络爬虫,亦称网页蜘蛛、网络机器人或网页追逐者,是指按照特定规则自动抓取万维网信息的程序或脚本。

以下提供一个示例代码供参考:

#! /usr/bin/env python

#
encoding = 'utf-8'#
Filename: spider_58center_sth.py
from bs4
import BeautifulSoup
import time
import requests
url_58 = 'http://nj.58.com/?PGTID=0d000000-0000-0c5c-ffba-71f8f3f7039e&ClickID=1'
''
'
用于爬取电商售卖信息: 例为58同城电脑售卖信息 ''
'
def get_url_list(url):
  web_data = requests.get(url)
soup = BeautifulSoup(web_data.text, 'lxml')
url = soup.select('td.t > a[class="t"]')
url_list = ''
for link in url:
  link_n = link.get('href')
if 'zhuanzhuan' in link_n:
  pass
else :
  if 'jump' in link_n:
  pass
else :
  url_list = url_list + '\n' + link_n
print('url_list: %s' % url_list)
return url_list# 分类获取目标信息
def get_url_info():
  url_list = get_url_list(url_58)
for url in url_list.split():
  time.sleep(1)
web_datas = requests.get(url)
soup = BeautifulSoup(web_datas.text, 'lxml')
type = soup.select('#head > div.breadCrumb.f12 > span:nth-of-type(3) > a')
title = soup.select(' div.col_sub.mainTitle > h1')
date = soup.select('li.time')
price = soup.select('div.person_add_top.no_ident_top > div.per_ad_left > div.col_sub.summary > ul > '
  'li:nth-of-type(1) > div.su_con > span.price.c_f50')
fineness = soup.select('div.col_sub.summary > u1 > li:nth-of-type(2) > div.su_con > span')
area = soup.select('div.col_sub.summary > u1 > li:nth-of-type(3) > div.su_con > span')
for typei, titlei, datei, pricei, finenessi, areai in zip(type, title, date, price, fineness, area): #做字典
data = {
  'type': typei.get_text(),
  'title': titlei.get_text(),
  'date': datei.get_text(),
  'price': pricei.get_text(),
  'fineness': (finenessi.get_text()).strip(),
  'area': list(areai.stripped_strings)
}
print(data)
get_url_info()

抓取商城商品售卖信息

总结

以上是关于Python网络爬虫抓取电商售卖信息代码示例的全部内容,希望能为读者提供帮助。感兴趣的朋友可继续查阅本站其他文章,如Python自定义实现线程池、ModelForm代码详解等。如有疏漏,欢迎留言指正,感谢支持。

本站声明

本文内容仅供参考,不代表本站观点。转载或引用请注明出处。如发现内容问题,请通过联系我们页面反馈。

Python 相关产品和应用

全部下载

专题列表