爬虫学习案例4

爬取猪八戒网站数据:2024-12-12

使用xpath解析元素,安装依赖库

powershell 复制代码
pip install lxml

使用selenium步骤我的上篇博客有提到,这里就不重复了
selenium使用博客导航

python 复制代码
# 安装pip install lxml,使用xpath
from lxml import etree
import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options

# 设置Chrome选项
chrome_options = Options()
chrome_options.add_argument("--headless")  # 无头模式,不打开浏览器窗口
chrome_options.add_argument("--disable-gpu")
chrome_options.add_argument("--no-sandbox")

# 设置ChromeDriver路径
service = Service('D:\\env\\python3\\chromedriver.exe')
keyword = "微信小程序"
url = f"https://www.zbj.com/fw/?k={keyword}"
# 初始化WebDriver
driver = webdriver.Chrome(service=service, options=chrome_options)
driver.get(url)
# 等待页面加载
time.sleep(2)  # 等待内容加载

html = driver.page_source # 原页面
# 使用xpath提取元素
tree = etree.HTML(html)
divList = tree.xpath("/html/body/div[2]/div/div/div[3]/div[1]/div[4]/div/div[2]/div[1]/div[2]/div")
for divItem in divList:
    price_elements = divItem.xpath("./div/div[3]/div[1]/span/text()")
    title_elements = divItem.xpath("./div/div[3]/div[2]/a/span/text()")
    company_elements = divItem.xpath("./div/div[5]/div/div/div/text()")
    sales_elements = divItem.xpath("./div/div[3]/div[3]/div[1]/div/span[2]/text()")
    good_elements = divItem.xpath("./div/div[3]/div[3]/div[2]/div/span[2]/text()")

    price = price_elements[0].strip("¥") if price_elements else "N/A"
    title = keyword.join(title_elements) if title_elements else "N/A"
    company = company_elements[0] if company_elements else "N/A"
    sales = sales_elements[0] if sales_elements else "N/A"
    good = good_elements[0] if good_elements else "N/A"

    print(f"价格: {price}")
    print(f"标题: {title}")
    print(f"商铺名: {company}")
    print(f"销量: {sales}")
    print(f"好评: {good}")
    print("下一家***********************")
driver.quit()  # 关闭浏览器

运行效果:

猪八戒网每次请求的数据都会随机打乱,所以控制台输出的顺序可能与页面的对应不上。

通过keyword变量可以更换查询内容。可以试试其他关键字。。。

相关推荐
炽烈小老头2 小时前
【每天学习一点算法 2026/03/08】相交链表
学习·算法·链表
IP搭子来一个3 小时前
爬虫IP地址受限怎么办?附解决方法
网络·爬虫·tcp/ip
red_redemption4 小时前
自由学习记录(130)
学习·soa·aos·ecs已成核心包·shading!=ps
双叶8364 小时前
(Python)Python爬虫入门教程:从零开始学习网页抓取(爬虫教学)(Python教学)
后端·爬虫·python·学习
天外来鹿5 小时前
Map/Set/WeakMap/WeakSet学习笔记
前端·javascript·笔记·学习
峥嵘life5 小时前
Android16 【GTS】 GtsDevicePolicyTestCases 测试存在Failed项
android·linux·学习
leixj0256 小时前
SVN学习笔记
笔记·学习·svn
毕设源码_廖学姐6 小时前
计算机毕业设计springboot古诗词学习App 基于SpringBoot的中华经典诗文数字化研习平台 SpringBoot框架下的传统诗词文化移动学习系统
spring boot·学习·课程设计
盐焗西兰花7 小时前
鸿蒙学习实战之路-Share Kit系列(7/17)-自定义分享面板操作区
linux·学习·harmonyos
香水5只用六神8 小时前
【RTOS快速入门】07_同步互斥与通信概述
单片机·嵌入式硬件·学习·操作系统·freertos·rtos·嵌入式软件