编写 爬取豆瓣TOP10 电影的 python脚本
时间: 2023-12-18 08:03:43 浏览: 133
非常好的问题!以下是实现豆瓣TOP10电影爬取的Python脚本示例:
```python
import requests
from bs4 import BeautifulSoup
url = "https://movie.douban.com/top250"
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"}
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.content, "html.parser")
movies = soup.find_all("div", class_="hd")
for movie in movies:
print(movie.find("span", class_="title").text)
```
希望对您有帮助!
相关问题
python爬取豆瓣电影top10
好的,以下是Python爬取豆瓣电影top10的代码:
```python
import requests
from bs4 import BeautifulSoup
url = 'https://movie.douban.com/top250'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.content, 'html.parser')
movie_list = soup.find(class_='grid_view').find_all('li')
for movie in movie_list[:10]:
title = movie.find(class_='title').text.strip()
rating = movie.find(class_='rating_num').text.strip()
print(title, rating)
```
代码中使用了requests库和BeautifulSoup库,首先获取豆瓣电影top250的页面内容,然后使用BeautifulSoup解析页面内容,找到class为grid_view的ul标签,再找到其中的每个li标签,获取电影的标题和评分,最后打印出前10个电影的标题和评分。
爬取豆瓣top250 python
为了爬取豆瓣Top250的电影信息,您可以使用Python中的Web爬虫库来实现。下面是一种基本的爬取方法:
```python
import requests
from bs4 import BeautifulSoup
def get_movies(url):
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36'
}
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, 'html.parser')
movie_list = soup.find_all('div', class_='hd')
movies = []
for movie in movie_list:
title = movie.a.span.text.strip()
movies.append(title)
return movies
def main():
url = 'https://movie.douban.com/top250'
movies = get_movies(url)
for i, movie in enumerate(movies):
print(f'{i+1}. {movie}')
if __name__ == '__main__':
main()
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)