python爬取一个电影网站http://www.dianyinggou.com案列
时间: 2024-01-20 16:02:05 浏览: 38
好的,下面是一个简单的 Python 爬取电影网站 http://www.dianyinggou.com 的示例代码,可以爬取电影名称、评分、导演、演员等信息:
```python
import requests
from bs4 import BeautifulSoup
url = 'http://www.dianyinggou.com/Top10.html'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
movies = soup.find_all('div', {'class': 'movie'})
for movie in movies:
title = movie.find('a').text.strip()
rating = movie.find('span', {'class': 'score'}).text.strip()
director = movie.find_all('p')[0].find_all('span')[1].text.strip()
actors = movie.find_all('p')[1].find_all('span')[1].text.strip()
print(f'电影名称:{title}')
print(f'评分:{rating}')
print(f'导演:{director}')
print(f'演员:{actors}')
print('-------------------------')
```
以上代码同样使用了 requests 库发送 HTTP 请求,使用了 BeautifulSoup 库解析 HTML 页面,通过 find 和 find_all 方法获取指定标签和属性的内容,最终将电影名称、评分、导演、演员等信息输出到控制台上。请注意,爬虫的使用需要遵守相关法律法规和网站的使用规定,不得用于非法用途。
相关推荐
![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_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)