selenium获取请求数据

python使用selenium获取请求头(Request Headers)

import json
from selenium import webdriver

caps = {
    'browserName': 'chrome',
    'loggingPrefs': {
        'browser': 'ALL',
        'driver': 'ALL',
        'performance': 'ALL',
    },
    'goog:chromeOptions': {
        'perfLoggingPrefs': {
            'enableNetwork': True,
        },
        'w3c': False,
    },
}
driver = webdriver.Chrome(desired_capabilities=caps)

driver.get('https://www.baidu.com')

for log in driver.get_log('performance'):
    if all(i in str(json.loads(log['message'])['message']['params']) for i in ('requestHeaders', 'User-Agent')):
        print(json.loads(log['message'])['message']['params']['response']['requestHeaders'])

driver.close()

selenium获取请求参数,在selenium加入下面两句,只能获取Post请求

import logging
logging.basicConfig(level = logging.DEBUG)
  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要使用PythonSelenium获取网络数据,你可以使用以下步骤: 1. 首先,确保你已经安装了Selenium库。你可以使用pip命令来安装它: ``` pip install selenium ``` 2. 导Selenium的WebDriver模块和相关的模块: ```python from selenium import webdriver from selenium.webdriver.chrome.service import Service from selenium.webdriver.common.by import By from selenium.webdriver.chrome.options import Options from selenium.webdriver.common.action_chains import ActionChains ``` 3. 配置和启动WebDriver,这里以Chrome为例: ```python # 设置Chrome选项 chrome_options = Options() chrome_options.add_argument("--headless") # 无界面模式 # 启动Chrome浏览器 selenium_service = Service('path/to/chromedriver') # 指定ChromeDriver的路径 driver = webdriver.Chrome(service=selenium_service, options=chrome_options) ``` 4. 打开需要抓取网络数据的网页: ```python driver.get('https://example.com') # 替换为你要访问的网页URL ``` 5. 获取网络数据,可以使用WebDriver提供的一些方法,比如: - 获取当前页面的源代码: ```python page_source = driver.page_source ``` - 获取当前页面的URL: ```python current_url = driver.current_url ``` - 获取所有的网络请求: ```python network_requests = driver.execute_script("return window.performance.getEntries()") ``` 这些方法可以根据你具体需要获取数据进行调用。 6. 关闭WebDriver: ```python driver.quit() ``` 请注意,Selenium是一个强大的工具,可以模拟用户在浏览器中的行为,包括点击、填写表单等操作。在使用Selenium抓取网络数据时,你可以结合其他的操作来模拟真实的用户行为,以获取更多有用的数据。同时,也要注意合法使用Selenium,遵守网站的使用规则和法律法规。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序烂人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值