本文主要是介绍python手写了个简易的豆瓣影评爬虫,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
使用python手写了个简易的豆瓣影评爬虫代码。
__author__ = 'wsx'import time
import requests
from bs4 import BeautifulSoup
import os
import re
import uuiddef 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 = 5000self.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 sdef 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 ;
这篇关于python手写了个简易的豆瓣影评爬虫的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!