import requests
import bs4
import time
# 定义请求头
headers = {
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) /'
'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36'
}
# 定义获取页面和解析数据方法
def getinfo(url):
response = requests.get(url,headers= headers)
soup = bs4.BeautifulSoup(response.text, 'lxml')
moives = []
# 基于class选择器获取元素
items = soup.select('div.hd > a > span:nth-child(1)')
for item in items:
name = item.string
moives.append([name,0])
# 基于find_all方法获取元素
scors = soup.find_all(class_='rating_num')
i = 0
for item in scors:
scor = float(item.string)
moives[i][1] =scor
i+=1
print(moives)
# 循环获取若干页面的电影名称和评分并打印
for page in range(5):
url = f'https://movie.douban.com/top250?start={page * 25}&filter='
print(f'第{page+1}页数据如下:')
getinfo(url)
time.sleep(1)
print('打印完毕!')
|