Python multithreading how to climb cat's eye movie
This article mainly introduces python multi-thread how to climb cat's eye movie, the article is very detailed, has a certain reference value, interested friends must finish!
Through the multithreading of python3, the top100 movie list of cat's eye movies is obtained, and the information is saved locally, using the requests library and regular expressions.
#! / usr/bin/env python#-*-coding: utf-8-*-# @ Author: Aiker Zhao# @ Date: 2:52 on 2019-4-2 AM# @ File: pamaoyan.py# @ Desc: import requestsfrom requests.exceptions import RequestExceptionimport reimport jsonfrom multiprocessing import Pooldef get_one_page (url): try: response = requests.get (url) if response.status_code = = 200: return response.text Return None except RequestException: return Nonedef parse_one_page (html): pattern = re.compile ('. *? board-index.*? > (\ d +). *? data-src= "(. *?)". *? name "> (. *?). *? star" > (. *?)
'+'. *? releasetime "> (. *?) (. *?). Re.S) pattern1 = re.compile ('board-index.*? > (\ d +). *? data-src=" (. *?) ". *? name" > (. *?). Star "> (. *?)
', re.S) items = re.findall (pattern, html) # print (items) for item in items: yield {' index': item [0], 'score': item [5] + item [6],' image': item [1], 'title': item [2],' actor': item [3] .strip () [3:] 'time': item [4] [5:]} def write_to_file (content): with open (' result.txt', 'asides, encoding='utf-8') as f: f.write (json.dumps (content) Ensure_ascii=False) +'\ n') f.close () def main (offset): url = 'https://maoyan.com/board/4?offset=' + str (offset) html = get_one_page (url) # print (html) for item in parse_one_page (html): print (item) write_to_file (item) if _ name__ = =' _ main__': # For i in range (10): # main (iTunes 10) pool = Pool () pool.map (main [I * 10 for i in range (10)]) these are all the contents of the article "how does python multithread climb Cat's Eye Movie" Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!