抓取猫眼电影排行
# -*- coding:utf-8 -*-
import requests
from requests.exceptions import RequestException
import re
import json
import time
# 抓取首页
def get_one_page(url):
try:
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)'
' Chrome/63.0.3239.26 Safari/537.36 Core/1.63.5478.400 QQBrowser/10.1.1550.400'
}
response = requests.get(url, headers=headers)
if response.status_code == 200: # 如果响应成功,返回响应的文本即html代码
return response.text
return None
except RequestException:
return None
# def main():
# url = 'http://maoyan.com/board/4'
# html = get_one_page(url)
# print(html)
# 正则提取
def parse_one_page(html):
pattern = re.compile('<dd>.*?board-index.*?>(\d+)</i>.*?board-img.*?src="(.*?)".*?name"><a'
'.*?>(.*?)</a>.*?star">(.*?)</p>.*?releasetime">(.*?)</p>'
'.*?integer">(.*?)</i>.*?fraction">(.*?)</i>.*?</dd>', re.S)
items = re.findall(pattern, html)
# print(items)
# ('1', '//ms0.meituan.net/mywww/image/loading_2.e3d934bf.png', '肖申克的救赎', '\n 主演:蒂姆·罗宾斯,摩根·弗里曼,鲍勃·冈顿\n ', '上映时间:1994-10-14(美国)', '9.', '5'),
for item in items:
yield{
'index': item[0],
'image': item[1],
'title': item[2].strip(),
'actor': item[3].strip()[3:], # 去掉 主演:
'time': item[4].strip()[5:], # 去掉 上映时间:
'score': item[5] + item[6] # 整数加小数
}
# 写入文件
def write_to_file(content):
with open('result.txt', 'a', encoding='utf-8')as f:
print(type(json.dumps(content)))
f.write(json.dumps(content, ensure_ascii=False)+'\n')
# 整合代码
# def main():
# url = 'http://maoyan.com/board/4'
# html = get_one_page(url)
# for item in parse_one_page(html):
# write_to_file(item)
# 分页爬取
def main(offset):
url = 'http://maoyan.com/board/4?offset=' + str(offset)
html = get_one_page(url)
for item in parse_one_page(html):
print(item)
write_to_file(item)
if __name__ == '__main__':
for i in range(10):
main(offset=i*10)
time.sleep(1)
部分结果
{'index': '1', 'image': '//ms0.meituan.net/mywww/image/loading_2.e3d934bf.png', 'title': '肖申克的救赎', 'actor': '蒂姆·罗宾斯,摩根·弗里曼,鲍勃·冈顿', 'time': '1994-10-14(美国)', 'score': '9.5'}
<class 'str'>
{'index': '3', 'image': '//ms0.meituan.net/mywww/image/loading_2.e3d934bf.png', 'title': '这个杀手不太冷', 'actor': '让·雷诺,加里·奥德曼,娜塔莉·波特曼', 'time': '1994-09-14(法国)', 'score': '9.5'}
<class 'str'>
{'index': '5', 'image': '//ms0.meituan.net/mywww/image/loading_2.e3d934bf.png', 'title': '泰坦尼克号', 'actor': '莱昂纳多·迪卡普里奥,凯特·温丝莱特,比利·赞恩', 'time': '1998-04-03', 'score': '9.5'}
<class 'str'>
...
...
...
{'index': '95', 'image': '//ms0.meituan.net/mywww/image/loading_2.e3d934bf.png', 'title': '黄金三镖客', 'actor': '克林特·伊斯特伍德,李·范·克里夫,埃里·瓦拉赫', 'time': '1966-12-23(意大利)', 'score': '8.9'}
<class 'str'>
{'index': '97', 'image': '//ms0.meituan.net/mywww/image/loading_2.e3d934bf.png', 'title': '我爱你', 'actor': '宋在浩,李顺才,尹秀晶', 'time': '2011-02-17(韩国)', 'score': '9.0'}
<class 'str'>
{'index': '99', 'image': '//ms0.meituan.net/mywww/image/loading_2.e3d934bf.png', 'title': '英雄本色', 'actor': '狄龙,张国荣,周润发', 'time': '2017-11-17', 'score': '9.2'}
<class 'str'>
思路:
- 首先抓取第一页的内容,用get_one_page()方法,得到源代码
- 观察要抓取内容的规律,构建正则表达式
- 定义解析页面的方法parse_one_page()
- 实现分页爬取
遇到的bug:unterminated subpattern at position 72,提示在if name == ‘main‘:一行出错,但实际是正则表达式部分写的有问题导致的
问题:为什么没有抓取全部电影,只是抓取网页中序号(不是index)是2、4、6…100的电影信息。