采集豆瓣电影的“选电影”网页中年代为“2023”、排序为“高分优先”筛选后前4页的电影名称和评分数据。网址:https://movie.douban.com/explore; 全程使用Selenium操控浏览器,不使用其他爬虫插件包和技术
时间: 2024-06-08 09:07:43 浏览: 16
以下是使用Python和Selenium实现的代码:
```python
from selenium import webdriver
import time
# 设置浏览器驱动路径
driver_path = 'chromedriver.exe'
# 创建Chrome浏览器对象
driver = webdriver.Chrome(executable_path=driver_path)
# 打开豆瓣电影“选电影”页面
url = 'https://movie.douban.com/explore'
driver.get(url)
# 选择年代为“2023”和排序为“高分优先”
driver.find_element_by_css_selector('#year > a:nth-child(7)').click()
driver.find_element_by_css_selector('#sort > ul > li:nth-child(3) > a').click()
# 获取前4页的电影名称和评分数据
for page in range(4):
print('第{}页:'.format(page + 1))
# 获取当前页面的电影名称和评分数据
elements = driver.find_elements_by_css_selector('.list-wp > a')
for element in elements:
name = element.find_element_by_css_selector('.title').text
rating = element.find_element_by_css_selector('.rate').text
print('{} {}'.format(name, rating))
# 点击“后页”按钮进入下一页
driver.find_element_by_css_selector('.list-pager > a:last-child').click()
time.sleep(1)
# 关闭浏览器
driver.quit()
```
这段代码首先使用Selenium打开豆瓣电影“选电影”页面,然后通过点击页面上的筛选条件,选择年代为“2023”和排序为“高分优先”。接着,通过循环遍历4页的电影列表,获取每个电影的名称和评分数据,并输出到控制台上。最后关闭浏览器。注意,由于豆瓣网站的反爬虫机制,为了避免被封IP,每次翻页需要等待1秒钟。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![](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)