python爬取豆瓣电影top250_python3爬取豆瓣top250电影
时间: 2023-07-03 12:20:48 浏览: 176
Python 爬取豆瓣电影Top250
好的,以下是Python3爬取豆瓣Top250电影的代码示例:
```python
import requests
from bs4 import BeautifulSoup
def get_movie_list(url):
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.text, 'html.parser')
movie_list = soup.find_all('div', class_='hd')
for i in range(len(movie_list)):
movie_name = movie_list[i].a.span.text.strip()
print(f'{i+1}. {movie_name}')
if __name__ == '__main__':
urls = ['https://movie.douban.com/top250?start={}'.format(str(i)) for i in range(0, 250, 25)]
for url in urls:
get_movie_list(url)
```
这段代码会爬取豆瓣Top250电影的名称,并且按顺序输出。你可以直接运行并查看结果。
阅读全文