selenium爬取安居客信息
时间: 2023-12-26 09:27:58 浏览: 135
以下是使用Selenium爬取安居客信息的步骤:
1. 导入必要的库和模块:
```python
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
```
2. 创建一个WebDriver对象,打开浏览器:
```python
driver = webdriver.Chrome()
```
3. 打开安居客网站:
```python
driver.get("https://www.anjuke.com/")
```
4. 在搜索框中输入关键词并提交搜索:
```python
search_box = driver.find_element_by_id("search-input")
search_box.send_keys("房屋出租")
search_box.send_keys(Keys.RETURN)
```
5. 等待页面加载完成:
```python
driver.implicitly_wait(10)
```
6. 定位并提取需要的信息:
```python
houses = driver.find_elements_by_class_name("house-item")
for house in houses:
title = house.find_element_by_class_name("house-title").text
price = house.find_element_by_class_name("house-price").text
print("标题:", title)
print("价格:", price)
print("--")
```
7. 关闭浏览器:
```python
driver.quit()
```
阅读全文