利用selenium爬取网易新闻
时间: 2023-09-04 15:15:46 浏览: 62
这是一个使用Python和Selenium爬取网易新闻的示例代码。在这个例子中,我们将使用Selenium模拟浏览器操作,浏览网易新闻的网页,然后提取所需的数据。
```python
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
# 指定Chrome浏览器驱动的路径
driver_path = '/path/to/chromedriver'
# 创建一个Chrome浏览器对象
driver = webdriver.Chrome(driver_path)
# 打开网易新闻首页
driver.get('https://www.163.com/')
# 等待5秒钟,让页面加载完成
time.sleep(5)
# 找到“要闻”栏目的链接,并点击
yaowen_link = driver.find_element_by_link_text('要闻')
yaowen_link.click()
# 等待5秒钟,让页面加载完成
time.sleep(5)
# 找到新闻列表的父元素
news_list = driver.find_element_by_class_name('area-half')
# 找到所有的新闻条目
news_items = news_list.find_elements_by_tag_name('li')
# 打印所有的新闻标题和链接
for item in news_items:
title = item.find_element_by_tag_name('a').text
url = item.find_element_by_tag_name('a').get_attribute('href')
print(title, url)
# 关闭浏览器
driver.quit()
```
在这个例子中,我们使用了Selenium的一些常见方法:
- `webdriver.Chrome()` 创建一个Chrome浏览器对象。
- `driver.get(url)` 打开指定的网页。
- `time.sleep(seconds)` 等待指定的时间,让页面加载完成。
- `driver.find_element_by_*()` 查找页面上的元素,例如 `find_element_by_link_text()` 查找包含指定文本的链接,`find_element_by_class_name()` 查找指定类名的元素等等。
- `element.text` 获取元素的文本内容。
- `element.get_attribute(attr)` 获取元素的指定属性值。
最后,我们打印了所有新闻条目的标题和链接。可以根据需要修改代码,提取不同的数据。