Python爬取豆瓣网前十名
时间: 2023-11-05 11:53:13 浏览: 92
好的,以下是Python代码,能够爬取豆瓣电影TOP250的前10名电影信息:
```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'}
movie_list = []
for i in range(0, 10):
link = url + '?start=' + str(i * 25)
r = requests.get(link, headers=headers)
soup = BeautifulSoup(r.text, 'lxml')
div_list = soup.find_all('div', class_='hd')
for each in div_list:
movie = each.a.span.text.strip()
movie_list.append(movie)
for i in range(0, 10):
print("Top" + str(i + 1) + ": " + movie_list[i])
```
运行该代码后,你会得到豆瓣电影TOP250的前10名电影名称。
阅读全文