python selenium current_url
时间: 2024-06-09 08:10:30 浏览: 4
在使用Python的selenium库时,可以通过`current_url`属性获取当前浏览器窗口的URL。具体的代码如下:
```python
from selenium import webdriver
# 创建一个Chrome浏览器实例
driver = webdriver.Chrome()
# 打开一个网页
driver.get("https://www.baidu.com/")
# 获取当前窗口的URL
current_url = driver.current_url
# 打印当前URL
print(current_url)
# 关闭浏览器
driver.quit()
```
需要注意的是,如果在打开页面后,页面发生了重定向,那么`current_url`属性获取的是重定向后的URL。
相关问题
python selenium获取network数据
要使用Python的Selenium库获取网络数据,你可以使用以下步骤:
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,遵守网站的使用规则和法律法规。
python selenium 跳转到新网站之后如何获取当前的url
可以使用Selenium中的current_url方法来获取当前的URL。具体做法是在跳转到新网站后,在driver对象上调用current_url方法即可获取当前URL。例如:
```
from selenium import webdriver
# 创建一个chrome浏览器对象
driver = webdriver.Chrome()
# 访问一个网站
driver.get("https://www.baidu.com")
# 点击一个链接跳转到新的网站
link = driver.find_element_by_link_text("新闻")
link.click()
# 获取当前的URL
current_url = driver.current_url
# 输出当前URL
print(current_url)
# 关闭浏览器
driver.quit()
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)