代码:
1 import json 2 from multiprocessing import Pool 3 import requests 4 # http://cn.python-requests.org/zh_CN/latest/_modules/requests/exceptions.html#RequestException 5 # RequestException是父类 6 from requests.exceptions import RequestException 7 import re 8 def get_one_page(url): 9 try: 10 response = requests.get(url) 11 if response.status_code == 200: 12 return response.text 13 return None 14 except RequestException: 15 print(RequestException.strerror) 16 return None 17 18 def parse_one_page(html): 19 # < dd > 20 # < i 21 # 22 # class ="board-index board-index-1" > 1 < / i > 23 # 排名 24 pattern = re.compile('<dd>.*?board-index.*?>(\d+)</i>.*?data-src="(.*?)".*?name"><a' 25 + '.*?>(.*?)</a>.*?star">(.*?)</p>.*?releasetime">(.*?)</p>' 26 + '.*?integer">(.*?)</i>.*?fraction">(.*?)</i>.*?</dd>', re.S) 27 28 #图片 29 # < img data - src = "https://p1.meituan.net/movie/20803f59291c47e1e116c11963ce019e68711.jpg@160w_220h_1e_1c" alt = "霸王别姬" class ="board-img" / > 30 items = re.findall(pattern, html) 31 # print(items) 32 for item in items: 33 yield { 34 'index': item[0], 35 'image': item[1], 36 'title': item[2], 37 'actor': item[3].strip()[3:], # 去掉 (主演:) 38 'time': item[4].strip()[5:], 39 'score': item[5]+item[6] 40 } 41 # 所以这个函数是一个迭代器 42 43 # 保存 44 def write_to_file(content): 45 with open('result.txt', 'a', encoding='utf-8') as f: 46 f.write(json.dumps(content, ensure_ascii=False) + '\n') # 47 f.close() 48 49 # https://maoyan.com/board/4?offset=20 第三页 50 def main(offset): 51 url = "https://maoyan.com/board/4?offset="+str(offset) 52 html = get_one_page(url) 53 54 for item in parse_one_page(html): 55 print(item) 56 write_to_file(item) 57 58 59 if __name__ == '__main__': 60 # for i in range(10): # top100 一共十页 61 # main(i*10) 62 63 # 多线程用法 64 pool = Pool() # list 65 pool.map(main, [i*10 for i i
执行结果(部分):
{'actor': '张国荣,张丰毅,巩俐', 'image': 'https://p1.meituan.net/movie/20803f59291c47e1e116c11963ce019e68711.jpg@160w_220h_1e_1c', 'title': '霸王别姬', 'time': '1993-01-01', 'index': '1', 'score': '9.5'} {'actor': '蒂姆·罗宾斯,摩根·弗里曼,鲍勃·冈顿', 'image': 'https://p0.meituan.net/movie/283292171619cdfd5b240c8fd093f1eb255670.jpg@160w_220h_1e_1c', 'title': '肖申克的救赎', 'time': '1994-09-10(加拿大)', 'index': '2', 'score': '9.5'} {'actor': '格利高里·派克,奥黛丽·赫本,埃迪·艾伯特', 'image': 'https://p0.meituan.net/movie/289f98ceaa8a0ae737d3dc01cd05ab052213631.jpg@160w_220h_1e_1c', 'title': '罗马假日', 'time': '1953-09-02(美国)', 'index': '3', 'score': '9.1'} {'actor': '让·雷诺,加里·奥德曼,娜塔莉·波特曼', 'image': 'https://p1.meituan.net/movie/6bea9af4524dfbd0b668eaa7e187c3df767253.jpg@160w_220h_1e_1c', 'title': '这个杀手不太冷', 'time': '1994-09-14(法国)', 'index': '4', 'score': '9.5'} {'actor': '莱昂纳多·迪卡普里奥,凯特·温丝莱特,比利·赞恩', 'image': 'https://p1.meituan.net/movie/b607fba7513e7f15eab170aac1e1400d878112.jpg@160w_220h_1e_1c', 'title': '泰坦尼克号', 'time': '1998-04-03', 'index': '5', 'score': '9.5'} {'actor': '周星驰,巩俐,郑佩佩', 'image': 'https://p0.meituan.net/movie/da64660f82b98cdc1b8a3804e69609e041108.jpg@160w_220h_1e_1c', 'title': '唐伯虎点秋香', 'time': '1993-07-01(中国香港)', 'index': '6', 'score': '9.1'} {'actor': '费雯·丽,罗伯特·泰勒,露塞尔·沃特森', 'image': 'https://p0.meituan.net/movie/46c29a8b8d8424bdda7715e6fd779c66235684.jpg@160w_220h_1e_1c', 'title': '魂断蓝桥', 'time': '1940-05-17(美国)', 'index': '7', 'score': '9.2'} {'actor': '费雯·丽,克拉克·盖博,奥利维娅·德哈维兰', 'image': 'https://p0.meituan.net/movie/223c3e186db3ab4ea3bb14508c709400427933.jpg@160w_220h_1e_1c', 'title': '乱世佳人', 'time': '1939-12-15(美国)', 'index': '8', 'score': '9.1'} {'actor': '寺田农,鹫尾真知子,龟山助清', 'image': 'https://p1.meituan.net/movie/ba1ed511668402605ed369350ab779d6319397.jpg@160w_220h_1e_1c', 'title': '天空之城', 'time': '1992', 'index': '9', 'score': '9.1'} {'actor': '连姆·尼森,拉尔夫·费因斯,本·金斯利', 'image': 'https://p0.meituan.net/movie/b0d986a8bf89278afbb19f6abaef70f31206570.jpg@160w_220h_1e_1c', 'title': '辛德勒的名单', 'time': '1993-12-15(美国)', 'index': '10', 'score': '9.2'} {'actor': '周星驰