python爬取js里的数据
时间: 2023-09-16 17:09:06 浏览: 47
要爬取 JavaScript 中的数据,需要使用 Python 的第三方库 Selenium 和 BeautifulSoup。
1. 安装 Selenium 和 BeautifulSoup。
```
pip install selenium
pip install beautifulsoup4
```
2. 下载并安装 Chrome 浏览器,并下载对应版本的 ChromeDriver。
3. 编写 Python 代码,启动 Chrome 浏览器,并打开需要爬取数据的网站。
```python
from selenium import webdriver
driver = webdriver.Chrome('/path/to/chromedriver')
driver.get('https://example.com')
```
4. 将页面中的 JavaScript 执行,并等待数据加载完成。
```python
import time
# 执行 JavaScript
driver.execute_script('window.scrollTo(0, document.body.scrollHeight);')
time.sleep(5) # 等待 5 秒钟,确保数据加载完成
```
5. 使用 BeautifulSoup 解析页面,获取需要的数据。
```python
from bs4 import BeautifulSoup
soup = BeautifulSoup(driver.page_source, 'html.parser')
data = soup.find('div', {'class': 'data'}).text # 假设需要获取 class 为 data 的 div 标签的文本内容
```
6. 关闭浏览器。
```python
driver.quit()
```
通过以上步骤,就可以使用 Python 爬取 JavaScript 中的数据了。注意,这种方式的爬取效率较低,建议只在必要的情况下使用。