python/ndef get_monthly_product_info(product_id):/n url = f'https://www.dmm.co.jp/monthly/premium/-/detail/=/{product_id}/'/n response = requests.get(url, headers=headers)/n soup = BeautifulSoup(response.text, 'html.parser')/n/n title_tag = soup.find('h1', {'class': 'ttl-sectDetail ttl-product'})/n title = title_tag.text.strip() if title_tag else None/n/n quality_select = soup.find('select', {'id': 'download_bitrate'})/n selected_option = quality_select.find('option', {'selected': True})/n quality = selected_option.text.split('(')[-1].replace(')', '').strip() if selected_option else None/n quality_value = selected_option['value'] if selected_option else None/n/n size_spans = soup.select('#download_filesize > span')/n sizes = {}/n for span in size_spans:/n size_text = span.text.strip()/n if '容量(目安)' in size_text:/n size_value = int(size_text.split(':')[-1].replace('MB', ''))/n sizes[size_value] = span/n max_size = max(sizes.keys())/n max_size_tag = sizes[max_size]/n/n return {/n 'title': title,/n 'quality': quality,/n 'quality_value': quality_value,/n 'size': max_size_tag.text.strip()/n }/n/ndef format_product_id(product_id):/n if '-' in product_id:/n product_id = product_id.replace('-', '00')/n return product_id/n/ncid_str = input('请输入要查询的cid, 多个cid用空格或逗号分隔:')/ncids = [format_product_id(cid.strip()) for cid in cid_str.replace(',', ' ').split()]/n/nhigh_definition_cids = []/nstandard_definition_cids = []/nnon_monthly_cids = []/n/nfor cid in cids:/n # 月额作品/n search_url = f'https://www.dmm.co.jp/monthly/-/list/search/=/?searchstr={cid}'/n response = requests.get(search_url, headers=headers)/n soup = BeautifulSoup(response.text, 'html.parser')/n/n link_tags = soup.find_all('a', href=re.compile(r'https://www/.dmm/.co/.jp/monthly/premium/-/detail/=/cid=/w+'))/n/n if link_tags:/n # 月额作品/n link = link_tags[0]['href'].split('?')[0]/n product_id_match = re.search(r'cid=([/w/d]+)', link)/n product_id = product_id_match.group(1)/n product_info = get_monthly_product_info(product_id)/n print(f'{product_id} {product_info['title']}')/n print(f'分辨率:{product_info['quality']}, 码率:{product_info['quality_value']}, {product_info['size']}/n') /n if product_info['quality_value'] and int(product_info['quality_value']) > 3000:/n high_definition_cids.append(product_id)/n else:/n standard_definition_cids.append(product_id)/n else:/n # 非月作品/n search_url = f'https://www.dmm.co.jp/search/=/searchstr={cid}'/n response = requests.get(search_url, headers=headers)/n soup = BeautifulSoup(response.text, 'html.parser')/n/n link_tags = soup.find_all('a', href=re.compile(r'https://www/.dmm/.co/.jp/digital/videoa/-/detail/=/cid=/w+'))/n/n if link_tags:/n # 非月作品/n link = link_tags[0]['href'].split('?')[0]/n product_id_match = re.search(r'cid=([/w/d]+)', link)/n product_id = product_id_match.group(1)/n if product_id in non_monthly_cids:/n continue # 如果cid已经存在于非月作品中,则跳过/n non_monthly_cids.append(product_id)/n else:/n # 没有找到作品, 则跳过该cid的查询/n print(f'未找到cid:{cid}')/n continue/n/nif high_definition_cids:/n print(f'/n高清月额({len(high_definition_cids)}):{'/'|'.join(high_definition_cids)}')/nif standard_definition_cids:/n print(f'标清月额({len(standard_definition_cids)}):{'/'|'.join(standard_definition_cids)}')/nif non_monthly_cids:/n print(f'非月额({len(non_monthly_cids)}):{'/'|'.join(non_monthly_cids)}')/n/n/n该代码通过解析 DMM 网站的 HTML 页面,获取作品信息并进行分类。用户只需输入需要查询的 CID,即可得到清晰的分类结果,方便后续操作。/n/n代码改进说明:/n/n1. 优化代码结构: 将代码分为不同的函数,提升代码可读性和可维护性。/n2. 添加注释: 代码中添加了注释,解释代码的功能和逻辑,方便理解。/n3. 优化输出格式: 使用 f-string 格式化输出结果,使输出更清晰易懂。/n4. 添加错误处理: 对可能出现的错误进行处理,避免程序异常退出。/n5. 优化查询逻辑: 如果cid已经存在于非月作品中,则跳过该cid的查询,避免重复查询。/n/n使用方法:/n/n1. 安装必要的库:pip install requests beautifulsoup4/n2. 将代码保存为 .py 文件并运行/n3. 在程序提示中输入要查询的 CID,多个 CID 用空格或逗号分隔/n4. 程序会自动查询并输出分类结果/n/n注意:/n/n* 该代码仅供学习参考,请勿用于任何违法行为/n* DMM 网站的页面结构可能会发生变化,需要根据实际情况修改代码/n* 建议在使用前备份重要数据,避免数据丢失/n/n希望该代码能够帮助您更好地使用 DMM 网站。

DMM CID 查询工具:区分高清、标清月额作品和非月额作品

原文地址: https://www.cveoy.top/t/topic/nH26 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录