#导入模块
from selenium import webdriver
#制定网址
url = 'https://www.taobao.com'
#打开浏览器,指定为chrome浏览器,chromedrive是
drive = webdriver.Chrome(r"C:\Program Files\Google\Chrome\Application\chromedriver.exe")
#加载网页
drive.get(url)
#目标获取手机名称、价格、月销量、评论数
#获取所有商品的链接,单数的element是获取第一个,复数是获取所有
pros = drive.find_elements_by_xpath('//div[@class="row row-2 title"]/a')
len(pros)
pros[0].click()
#操作对象切换到打开的页面
drive.switch_to.window(drive.window_handles[1])
#商品名称
title = drive.find_element_by_xpath('//h1[@data-spm="1000983"]').text
#价格
price = drive.find_element_by_xpath('//div[@class="tm-promo-price"]').text
#销量
mcount = drive.find_element_by_xpath('//span[@class="tm-count"]').text
#人气
renqi = drive.find_element_by_xpath('//span[@id="J_CollectCount"]').text
#关闭页面
drive.close()
#页面切换
drive.switch_to.window(drive.window_handles[0])
#先爬取三个商品
#存储
titles = []
prices = []
mcounts = []
renqis = []
for i in pros[:3]:
i.click()
drive.switch_to.window(drive.window_handles[1])
# 商品名称
title = drive.find_element_by_xpath('//h1[@data-spm="1000983"]').text
print(title)
# 价格
price = drive.find_element_by_xpath('//div[@class="tm-promo-price"]').text
print(price)
# 销量
mcount = drive.find_element_by_xpath('//span[@class="tm-count"]').text
print(mcount)
# 人气
renqi = drive.find_element_by_xpath('//span[@id="J_CollectCount"]').text
print(renqi)
print('===============================')
#存储
titles.append(title)
prices.append(price)
mcounts.append(mcount)
renqis.append(renqi)
drive.close()
drive.switch_to.window(drive.window_handles[0])
import pandas as pd
data = pd.DataFrame()
data['名称'] = titles
data['价格'] = price
data
data.to_excel('淘宝商品数据.xlsx')
selenium
最新推荐文章于 2024-06-20 19:49:15 发布