python 爬取豆瓣电影排名
任务要求
- 获取豆瓣电影排名前250信息
- 打包进文件夹
代码展示
代码如下
import requests#导入requests库
import re
def GetHtml(url):
try:
RequestAddrss={'user-agent':'Mozilla/5.0'}#更改请求地址为Mozilla/5.0
r=requests.get(url,headers = RequestAddrss)#获取网页源码
r.raise_for_status()#捕获异常
r.encoding=r.apparent_encoding#更换编码集
return r.text#获得字符串形式网页源码
except:
return '请求错误'
def GetRank(Html):#获取网页源码
data_re=' <em class="">(?P<rank>.+?)</em>.+?<span class="title">(?P<title>.+?)</span>.+?\
<span class="rating_num" property="v:average">(?P<score>.+?)</span>.+?\
<span>(?P<count>.+?)评价</span>'
data=re.compile(data_re,re.S)#re.S模式为.匹配所有字符
return data.finditer(Html)
def Save(rank):#获取当前源码所有电影排名,并储存在Movie_rankings.txt中
for ele in rank:
rank=ele.group('rank')
name=ele.group('title')
score=ele.group('score')
number=ele.group('count')
data_list=[{'rank':rank},{'name':name},{'score':score},{'number':number}]
with open('Movie_rankings.txt','a',encoding='utf-8') as f:
f.write('\n'+str(data_list))
def main():
N=0
for i in range(10):#遍历所有排名页面
url='https://movie.douban.com/top250?start=%s&filter='%N
Html=GetHtml(url)
rank=GetRank(Html)
Save(rank)
N=N+25
main()
运行结果