江苏网站seo营销模板,网页设计与制作软件下载,网站源码本地演示,企业seo网站推广公司爬取猪八戒网站数据#xff1a;2024-12-12
使用xpath解析元素#xff0c;安装依赖库
pip install lxml使用selenium步骤我的上篇博客有提到#xff0c;这里就不重复了 selenium使用博客导航
# 安装pip install lxml,使用xpath
from lxml import etree
import time
from s…爬取猪八戒网站数据2024-12-12
使用xpath解析元素安装依赖库
pip install lxml使用selenium步骤我的上篇博客有提到这里就不重复了 selenium使用博客导航
# 安装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 fhttps://www.zbj.com/fw/?k{keyword}
# 初始化WebDriver
driver webdriver.Chrome(serviceservice, optionschrome_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/Atitle keyword.join(title_elements) if title_elements else N/Acompany company_elements[0] if company_elements else N/Asales sales_elements[0] if sales_elements else N/Agood good_elements[0] if good_elements else N/Aprint(f价格: {price})print(f标题: {title})print(f商铺名: {company})print(f销量: {sales})print(f好评: {good})print(下一家***********************)
driver.quit() # 关闭浏览器
运行效果 猪八戒网每次请求的数据都会随机打乱所以控制台输出的顺序可能与页面的对应不上。 通过keyword变量可以更换查询内容。可以试试其他关键字。。。