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

爬虫 专栏收录该内容
7 篇文章 0 订阅

**

爬虫,爬取猫眼电影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(
                    '<p class="score"><i class="integer">(\d\.)</i><i class="fraction">(\d)</i></p>',
                    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')

输出:在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值