爬取豆瓣电影top250:
import requests
import re
'''
第一页:
https://movie.douban.com/top250?start=0&filter=
第二页:
https://movie.douban.com/top250?start=25&filter=
第三页:
https://movie.douban.com/top250?start=50&filter=
第十页:
https://movie.douban.com/top250?start=225&filter=
'''
0获取所有电影的URL
num=0
for line in range(10):
url=f'https://movie.douban.com/top250?start={num}&filter='
num+=25
#print(url)
# 1.发送请求
response=requests.get(
url=url
)
# 获取响应文本
#print(response.text)
'''
1.电影名称
2.电影详情页链接
3.电影评分
4.电影评价人数
'''
# 2.解析并提取数据
# 查找所有
# re.S: 全局查找
# re.findall('正则匹配规则', '解析文本', re.S)
# 获取所有电影的名称
# movie_name = re.findall('<div class="item">.*?<span class="title">(.*?)</span>', response.text, re.S)
# 获取电影的名称与详情页地址
# movie_name = re.findall('<div class="item">.*?<a href="(.*?)">.*?<span class="title">(.*?)</span>', response.text, re.S)
movie_list = re.findall('<div class="item">.*?<a href="(.*?)">.*?<span class="title">(.*?)</span>.*?<span class="rating_num" property="v:average">(.*?)</span>.*?<span>(.*?)人评价</span>',
response.text, re.S)
#movie_list = re.findall('<div class="item">.*?<a href="(.*?)">.*?<span class="title">(.*?)</span>.*?<span class="rating_num" property="v:average">(.*?)</span>.*?<span>(.*?)人评价</span>',response.text, re.S)
#print(movie_list)
# 循环
num = 1
with open('douban.txt', 'a', encoding='utf-8') as f:
for line in movie_list:
movie_url = line[0]
movie_name = line[1]
movie_point = line[2]
movie_count = line[3]
f.write(movie_url + '---' + movie_name + '---' + movie_point + '---' + movie_count + '\n')
#print(movie_url + '---' + movie_name + '---' + movie_point + '---' + movie_count + '\n')