python手写了个简易的豆瓣影评爬虫

使用python手写了个简易的豆瓣影评爬虫代码。

__author__ = 'wsx'

import time
import requests
from bs4 import BeautifulSoup
import os
import re
import uuid


def clean_windows_filename(string_file_name):
    invalid_chars = r'[\\/:*?"<>|]'
    return re.sub(invalid_chars, '', string_file_name)
class Mz:



    def __init__(self):
        # self.url = 'http://www.mzitu.com'
        # self.headers = {
        #     'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36',
        #     'Referer': 'http://www.mzitu.com/'
        # }
        self.url = 'https://nj9.net/id/'
        self.headers = {
            'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36',
            'Referer': 'https://nj9.net'
        }
        self.proxy_list = {
                'http': 'http://47.56.110.204:8989',
                'https': 'https://65.109.204.150:80'
            }
        self.req = requests.session()
        self.all_a = 5000
        self.all_a_title = []
        self.all_a_max = []
        try:
            os.makedirs(os.path.join(os.getcwd(), 'douban'))
            os.chdir(os.path.join(os.getcwd(), 'douban'))
        except OSError as e:
            # 处理其他类型的错误
            print("已存在文件,跳过创建文件!")
        finally:
            self.initpwd = os.getcwd()

    def contains_unfound(s):
        return "未找到" in s


    def Downloadimg2(self):
            nurl = "https://movie.douban.com/subject/36328210/comments"
            html = self.req.get(nurl, headers=self.headers)
            title = BeautifulSoup(html.text, 'lxml').find('h1').text
            # 定义不允许的字符
            invalid_chars = '<>:"/\\|?*'
            # 使用正则表达式替换所有不允许的字符为空字符串
            sanitized = re.sub(f'[{re.escape(invalid_chars)}]', '', title)
            if "未找到" in title:
                print("未找到")
            else:
                try:
                    os.makedirs(os.path.join(os.getcwd(), sanitized))
                except OSError as e:
                    # 处理其他类型的错误
                    print(title + "已存在文件,跳过创建文件!")
                finally:
                    os.chdir(os.path.join(os.getcwd(), sanitized))
                shortUrl = BeautifulSoup(html.content, 'lxml')
                # 查找所有的<img>标签
                short_tags = shortUrl.find_all('span', class_='short')
                # 获取每个<img>标签的src属性
                all_short = [short.text for short in short_tags]
                # 打印获取到的所有短评
                for short in all_short:
                    print(short)
                    # 打开文件并写入文本
                    txt_url = os.getcwd() + "\\" + sanitized+str(".txt")
                    with open(txt_url, 'a', encoding='utf-8') as file:
                        file.write(short)
                        file.write("\n\n")
            os.chdir(self.initpwd)
            print('Dowmload completed!')


if __name__ == '__main__':
    test = Mz()
    test.Downloadimg2()

备注:如果需要其他电影评论,需替换代码中nurl ;

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值