python+selenium 活动页面监控(获取活动页面商品链接,商品名称价格,自动加购核算总价)

from selenium import webdriver
import time
import json

wb = webdriver.Chrome()
wb.maximize_window()
wb.implicitly_wait(10)

url = 'https://subject.autohome.com.cn/carservice/2019/12/hnzt/?refpage=18518&exl_pvid=6833672&exl_hdid=18518&pvareaid=3281150'
wb.get(url)

# 提前add了一份cookie,直接打开保存的cookie文件
with open('D:\\qichecookies.txt', 'r', encoding='utf8') as f:
    listCookies = json.loads(f.read())
for cookie in listCookies:
    wb.add_cookie(cookie)
# 读取完cookie刷新页面
wb.refresh()

# 找到所有商品的链接
link = wb.find_elements_by_css_selector('.w>li>a')
links = []
for i in link:
    if 'https' in i.get_attribute('href'):
        links.append(i.get_attribute('href'))
print(links)

text = []
price = []
# 商品详情取标题,价格,加购物车
for i in links:
    try:
        wb.get(i)
        time.sleep(5)
        text.append(wb.find_element_by_css_selector('#detailHeader>h1').text)
        price.append(wb.find_element_by_id('price').text)
        try:
            wb.find_element_by_name('normal').click()
            wb.find_element_by_id('addCart').click()
            time.sleep(3)
        except:
            print(wb.find_element_by_css_selector('#detailHeader>h1').text,'暂不支持销售')
    except Exception as e:
        print(e)

print(text)
print(price)

# 购物车页面取商品总价
wb.get(wb.find_element_by_class_name('nav-right').get_attribute('href'))
time.sleep(3)
total = wb.find_element_by_id('allPrice')
print('商品总价:', total.text)

 

上一篇:JAVA:Excel文档转无水印PDF文档


下一篇:厦门房地产网签备案的图片识别之二