selenium使用 动态页面分析

本文详细介绍了如何使用Selenium进行动态网页的分析,包括等待元素加载、捕捉动态变化、模拟用户交互等关键步骤,旨在帮助开发者更有效地进行自动化测试和网页数据抓取。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import re
import time

from selenium import webdriver
from selenium.common.exceptions import WebDriverException
from selenium.webdriver.chrome.options import Options

# from selenium.webdriver.common.touch_actions import TouchActions

"""方案1:手机模式"""
mobile_emulation = {
    # "deviceName": "Apple iPhone 3GS"
    # "deviceName": "Apple iPhone 4"
    # "deviceName": "Apple iPhone 5"
    # "deviceName": "Apple iPhone 6"
    # "deviceName": "Apple iPhone 6 Plus"
    # "deviceName": "BlackBerry Z10"
    # "deviceName": "BlackBerry Z30"
    # "deviceName": "Google Nexus 4"
    # "deviceName": "Google Nexus 5"
    # "deviceName": "Google Nexus S"
    # "deviceName": "HTC Evo, Touch HD, Desire HD, Desire"
    # "deviceName": "HTC One X, EVO LTE"
    # "deviceName": "HTC Sensation, Evo 3D"
    # "deviceName": "LG Optimus 2X, Optimus 3D, Optimus Black"
    # "deviceName": "LG Optimus G"
    # "deviceName": "LG Optimus LTE, Optimus 4X HD"
    # "deviceName": "LG Optimus One"
    # "deviceName": "Motorola Defy, Droid, Droid X, Milestone"
    # "deviceName": "Motorola Droid 3, Droid 4, Droid Razr, Atrix 4G, Atrix 2"
    # "deviceName": "Motorola Droid Razr HD"
    # "deviceName": "Nokia C5, C6, C7, N97, N8, X7"
    # "deviceName": "Nokia Lumia 7X0, Lumia 8XX, Lumia 900, N800, N810, N900"
    # "deviceName": "Samsung Galaxy Note 3"
    # "deviceName": "Samsung Galaxy Note II"
    # "deviceName": "Samsung Galaxy Note"
    # "deviceName": "Samsung Galaxy S III, Galaxy Nexus"
    # "deviceName": "Samsung Galaxy S, S II, W"
    # "deviceName": "Samsung Galaxy S4"
    # "deviceName": "Sony Xperia S, Ion"
    # "deviceName": "Sony Xperia Sola, U"
    # "deviceName": "Sony Xperia Z, Z1"
    # "deviceName": "Amazon Kindle Fire HDX 7″"
    # "deviceName": "Amazon Kindle Fire HDX 8.9″"
    # "deviceName": "Amazon Kindle Fire (First Generation)"
    # "deviceName": "Apple iPad 1 / 2 / iPad Mini"
    # "deviceName": "Apple iPad 3 / 4"
    # "deviceName": "BlackBerry PlayBook"
    # "deviceName": "Google Nexus 10"
    # "deviceName": "Google Nexus 7 2"
    # "deviceName": "Google Nexus 7"
    # "deviceName": "Motorola Xoom, Xyboard"
    # "deviceName": "Samsung Galaxy Tab 7.7, 8.9, 10.1"
    # "deviceName": "Samsung Galaxy Tab"
    # "deviceName": "Notebook with touch"
    "deviceName": "iPhone 6"
    # Or specify a specific build using the following two arguments
    # "deviceMetrics": { "width": 360, "height": 640, "pixelRatio": 3.0 },
    # "userAgent": "Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 5 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19" }
}
chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option('mobileEmulation', mobile_emulation)
_chrom = webdriver.Chrome(chrome_options=chrome_options)
"""方案2:电脑浏览器模式"""
# _chrom = webdriver.Chrome()

_chrom.get('https://p.blackfish.cn/p/m-vipPromotion-daylist?shareCode=a5f67ed84522ae23&detail='
           '{"inviteTime":"20190120","dayCount":397,"memberType":1}&from=groupmessage&isappinstalled=0')
try:
    _chrom.set_window_size(426, 800)
except WebDriverException as e:
    print(e)

# 滑动
# 参考文档1:https://www.jianshu.com/p/9e9e8a67ef86
# 参考文档2:https://www.cnblogs.com/mengyu/p/8136421.html
# 参考文档3:https://zhuanlan.zhihu.com/p/28710941
# 参考文档4:https://blog.youkuaiyun.com/Teamo_mc/article/details/81557885
# 参考文档5:https://www.cnblogs.com/mengyu/p/8136421.html

try:
    """定位操作元素"""
    button = _chrom.find_element_by_xpath('/html/body/div/div/div/div/div[12]')
    print(button.text)
    """方案1:"""
    # action.flick_element(button, 0, -600, 200).perform()  # 是向上了,但是没有效果
    # time.sleep(5)
    """方案2: 是下拉了,但是没有加载手机号码,不知道为什么
    """
    # action = webdriver.TouchActions(_chrom)
    # action.scroll_from_element(button, 0, 600)  # 是向上了,但是没有效果
    # action.perform()
    # time.sleep(5)
    """方案3: 没有效果
    """
    action = webdriver.TouchActions(_chrom)
    action.long_press(button)
    action.move(213, 100)
    action.perform()
    time.sleep(5)
    """方案4:"""
    # Action.tap_and_hold(213, 600).perform()
    # print("11")
    # Action.move(213, 0).perform()  # 失败了,连移动的感觉都没有
    # print("22")
    # Action.release(213, 0).perform()
except WebDriverException as e:
    print(e)
_html = _chrom.page_source
# 正则表达式抽取数据
pattern = re.compile(r'[0-9]{3}\*\*\*\*[0-9]{4}', re.M)
myTable = re.findall(pattern, _html)

print(myTable)
# time.sleep(50)
_chrom.close()

os.system('taskkill /im chromedriver.exe /F')
os.system('taskkill /im chrome.exe /F')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值