爬虫,爬取猫眼电影Top100的电影名与评分

**

爬虫,爬取猫眼电影Top100的电影名与评分

**

import requests
import threading
import re

class maoyan_top500(threading.Thread):
    def __init__(self, start_, end_,lock):
        threading.Thread.__init__(self)
        self.headers = {
            'User-Agent':
            'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36'
        }
        self.base_url = 'https://maoyan.com/board/4?offset=%d'
        self.start_ = start_
        self.end_ = end_
        self.lock = lock

    def run(self):
        for offset in range(self.start_, self.end_, 10):
            url = self.base_url % offset
            response = requests.get(url, headers=self.headers)
            html = response.text
            info_list = self.get_Information(html)
            with self.lock:
                self.write(info_list)
            print('offset {} OK !'.format(offset))

    def get_Information(self, html):
        information_list = []
        for line in html.split('\n'):
            if 'class="image-link"' in line:
                movie_name = line.split('title="')[1].split('"')[0]
                information_list.append(movie_name)
            if 'class="integer"' in line:
                res = re.search(
                    '

(\d\.)(\d)

'
, line) integer = res.group(1) fraction = res.group(2) score = integer + fraction information_list.append(score) return information_list def write(self,info_list): str_ = str(info_list) + '\n' with open('D:\\mixhhh\\邮箱\\电影\\top5000.txt',mode='a',encoding='utf8') as file: file.write(str_) if __name__ == "__main__": threads = [] lock = threading.Lock() for i in range(2): t = maoyan_top500(start_=i * 50, end_=(i + 1) * 50,lock=lock) t.start() threads.append(t) for t in threads: t.join() print('Over')

输出:爬虫,爬取猫眼电影Top100的电影名与评分_第1张图片
爬虫,爬取猫眼电影Top100的电影名与评分_第2张图片

你可能感兴趣的:(爬虫)