当前位置: 首页 > news >正文

2.4 playwright 实战-爬取某宝商品信息

第四节:电商信息爬取项目实战项目

课程目标

  • 学习如何通过playwright完成某宝商品信息爬取

课程内容

编码实现
import json
from playwright.sync_api import Playwright,sync_playwright
# playwright = sync_playwright()
import time
from tqdm import tqdm
import pandas as pd
def run(playwright:Playwright) -> None:browser = playwright.chromium.launch(headless=False)context = browser.new_context()# context = browser.new_context()page = context.new_page()page.goto("https://uland.taobao.com/sem/tbsearch")# 搜索# 请输入搜索文字serch_input = page.get_by_label("请输入搜索文字")# serch_input.click()serch_input.fill("工装裤")# 搜索按钮serch_button = page.get_by_role("button",name="搜索")serch_button.click()commoditie_infos = []for i in range(5):print(f"第{i+1}页")page.mouse.wheel(0, 8000) # 将滚动条拉到最下保证数据加载 page.wait_for_selector(".Card--doubleCard--wznk5U4") # 这个类可以被选择意味着加载完毕element = page.locator(".Card--doubleCard--wznk5U4").locator("..")commodities = element.all()for commoditie in tqdm(commodities):title_element = commoditie.locator(".Title--title--jCOPvpf")title_element.wait_for()title = title_element.inner_text() # 标题img_element = commoditie.locator(".MainPic--mainPic--rcLNaCv")try:img_element.wait_for(timeout=500,state="attached")img = img_element.get_attribute("src") except:img= ""# 价格price_int_element = commoditie.locator(".Price--priceInt--ZlsSi_M")price_int_element.wait_for()price_int = price_int_element.inner_text()price_float_element = commoditie.locator(".Price--priceFloat--h2RR0RK")price_float_element.wait_for()price_float = price_float_element.inner_text()price = price_int + price_floatprice = float(price)# 发货地deliver_place_elements = commoditie.locator(".Price--procity--_7Vt3mX").all()# deliver_place_elements = deliver_element.all()deliver_place_father_element = deliver_place_elements[0]deliver_place_father_element.wait_for()deliver_place_father = deliver_place_father_element.inner_text()if len(deliver_place_elements) == 1:deliver_place_son = ""else:deliver_place_son_element = deliver_place_elements[1]deliver_place_son_element.wait_for()deliver_place_son = deliver_place_son_element.inner_text()deliver_place = deliver_place_father + deliver_place_son# 品牌brand_element = commoditie.locator(".ShopInfo--shopName--rg6mGmy")brand_element.wait_for()brand = brand_element.inner_text()commoditie_infos.append({"title":title,"img":img,"price":price,"deliver_place":deliver_place,"brand":brand,})page.get_by_label("下一页").click()df = pd.DataFrame(commoditie_infos)df.to_csv("商品7.csv",encoding="gbk",index=False)with sync_playwright() as playwright:run(playwright)
测试与调试
  • 测试
    • 测试程序的各个功能是否正常工作。
  • 调试
    • 根据测试结果,调整和优化代码。

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 四款录屏大师,一键搞定!新手也能快速上手?
  • Python数值计算(24)——PCHIP
  • Chapter 9 Operational Amplifiers
  • 快速上手Spring Boot
  • 6G:融合5G与AI,重塑网络交互与智能决策的未来
  • NB模组AT 命令用法记录
  • 如何使用yolov5-master进行训练
  • 书生.浦江大模型实战训练营——(四)书生·浦语大模型全链路开源开放体系
  • JavaScript高阶笔记总结第三天:(JavaScript高阶完结)
  • JavaScript中的字符串与数字转换
  • 人工智能GPU算力评估分析
  • Unity动画模块 之 Animation
  • Gradle相关的语法
  • 官宣|Apache Flink 1.20 发布公告
  • linux系统编程:缓冲区,文件io(19)
  • 「面试题」如何实现一个圣杯布局?
  • android百种动画侧滑库、步骤视图、TextView效果、社交、搜房、K线图等源码
  • CEF与代理
  • Go 语言编译器的 //go: 详解
  • iOS 系统授权开发
  • Java方法详解
  • Terraform入门 - 1. 安装Terraform
  • Web标准制定过程
  • 闭包--闭包作用之保存(一)
  • 服务器从安装到部署全过程(二)
  • 紧急通知:《观止-微软》请在经管柜购买!
  • 来,膜拜下android roadmap,强大的执行力
  • 实现简单的正则表达式引擎
  • 数组大概知多少
  • 突破自己的技术思维
  • 移动端解决方案学习记录
  • 原创:新手布局福音!微信小程序使用flex的一些基础样式属性(一)
  • ​第20课 在Android Native开发中加入新的C++类
  • #我与Java虚拟机的故事#连载03:面试过的百度,滴滴,快手都问了这些问题
  • (145)光线追踪距离场柔和阴影
  • (3)选择元素——(17)练习(Exercises)
  • (6)添加vue-cookie
  • (带教程)商业版SEO关键词按天计费系统:关键词排名优化、代理服务、手机自适应及搭建教程
  • (二)pulsar安装在独立的docker中,python测试
  • (附源码)spring boot校园健康监测管理系统 毕业设计 151047
  • (附源码)计算机毕业设计ssm基于B_S的汽车售后服务管理系统
  • (个人笔记质量不佳)SQL 左连接、右连接、内连接的区别
  • (六)vue-router+UI组件库
  • (三)centos7案例实战—vmware虚拟机硬盘挂载与卸载
  • (四)Tiki-taka算法(TTA)求解无人机三维路径规划研究(MATLAB)
  • (贪心) LeetCode 45. 跳跃游戏 II
  • (学习日记)2024.04.10:UCOSIII第三十八节:事件实验
  • (终章)[图像识别]13.OpenCV案例 自定义训练集分类器物体检测
  • (转)chrome浏览器收藏夹(书签)的导出与导入
  • (转)scrum常见工具列表
  • .NET HttpWebRequest、WebClient、HttpClient
  • .NET 中创建支持集合初始化器的类型
  • .Net接口调试与案例
  • .NET企业级应用架构设计系列之结尾篇
  • .Net中的集合