爬取豆瓣top250 python
时间: 2023-11-06 12:08:00 浏览: 108
python爬取豆瓣top250
5星 · 资源好评率100%
为了爬取豆瓣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()
```
阅读全文