【爬虫记录-Tesla迎宾灯货源补货自动提示】 基于python+browsermobproxy+selenium的爬虫+自动发送邮件提醒
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from browsermobproxy import Server
import time
# 启动Browsermob Proxy
server = Server('path/to/browsermob-proxy')
server.start()
proxy = server.create_proxy()
# 启动Webdriver
proxy.new_har("TeslaYuYue", options={'captureHeaders': True})
driver_options = webdriver.ChromeOptions()
driver_options.add_argument('--proxy-server={0}'.format(proxy.proxy))
driver = webdriver.Chrome(executable_path='path/to/chromedriver', options=driver_options)
# 打开网页
driver.get("https://www.tesla.com/")
# 执行特定操作,例如搜索
search_field = driver.find_element(By.ID, "search")
search_field.send_keys("Model 3")
search_field.send_keys(Keys.RETURN)
# 等待页面加载完成
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "product_grid")))
# 获取HAR数据
time.sleep(5) # 确保代理服务器捕获所有网络流量
har_entries = proxy.har
# 分析HAR数据并进行处理
for entry in har_entries['log']['entries']:
if "stock" in entry['request']['url']:
# 发送提醒或其他处理
print("发现Model 3库存信息请求")
# 清理工作
proxy.close()
server.stop()
driver.quit()
这个示例代码使用了Browsermob-Proxy和Selenium来监控和分析访问特定网站时的网络流量,并在发现特定请求(例如,关于特定产品库存信息的请求)时进行处理。这种方法可以用来监测和预警未来可能的商品供应情况,从而实现自动化提醒。
评论已关闭