xiaohongshu.py 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import functools
  2. from .common import InfoExtractor
  3. from ..utils import (
  4. float_or_none,
  5. int_or_none,
  6. js_to_json,
  7. url_or_none,
  8. )
  9. from ..utils.traversal import traverse_obj
  10. class XiaoHongShuIE(InfoExtractor):
  11. _VALID_URL = r'https?://www\.xiaohongshu\.com/explore/(?P<id>[\da-f]+)'
  12. IE_DESC = '小红书'
  13. _TESTS = [{
  14. 'url': 'https://www.xiaohongshu.com/explore/6411cf99000000001300b6d9',
  15. 'md5': '2a87a77ddbedcaeeda8d7eae61b61228',
  16. 'info_dict': {
  17. 'id': '6411cf99000000001300b6d9',
  18. 'ext': 'mp4',
  19. 'uploader_id': '5c31698d0000000007018a31',
  20. 'description': '#今日快乐今日发[话题]# #吃货薯看这里[话题]# #香妃蛋糕[话题]# #小五卷蛋糕[话题]# #新手蛋糕卷[话题]#',
  21. 'title': '香妃蛋糕也太香了吧🔥不需要卷❗️绝对的友好',
  22. 'tags': ['今日快乐今日发', '吃货薯看这里', '香妃蛋糕', '小五卷蛋糕', '新手蛋糕卷'],
  23. 'duration': 101.726,
  24. 'thumbnail': r're:https?://sns-webpic-qc\.xhscdn\.com/\d+/[a-z0-9]+/[\w]+',
  25. },
  26. }]
  27. def _real_extract(self, url):
  28. display_id = self._match_id(url)
  29. webpage = self._download_webpage(url, display_id)
  30. initial_state = self._search_json(
  31. r'window\.__INITIAL_STATE__\s*=', webpage, 'initial state', display_id, transform_source=js_to_json)
  32. note_info = traverse_obj(initial_state, ('note', 'noteDetailMap', display_id, 'note'))
  33. video_info = traverse_obj(note_info, ('video', 'media', 'stream', ('h264', 'av1', 'h265'), ...))
  34. formats = []
  35. for info in video_info:
  36. format_info = traverse_obj(info, {
  37. 'fps': ('fps', {int_or_none}),
  38. 'width': ('width', {int_or_none}),
  39. 'height': ('height', {int_or_none}),
  40. 'vcodec': ('videoCodec', {str}),
  41. 'acodec': ('audioCodec', {str}),
  42. 'abr': ('audioBitrate', {int_or_none}),
  43. 'vbr': ('videoBitrate', {int_or_none}),
  44. 'audio_channels': ('audioChannels', {int_or_none}),
  45. 'tbr': ('avgBitrate', {int_or_none}),
  46. 'format': ('qualityType', {str}),
  47. 'filesize': ('size', {int_or_none}),
  48. 'duration': ('duration', {functools.partial(float_or_none, scale=1000)}),
  49. })
  50. formats.extend(traverse_obj(info, (('mediaUrl', ('backupUrls', ...)), {
  51. lambda u: url_or_none(u) and {'url': u, **format_info}})))
  52. thumbnails = []
  53. for image_info in traverse_obj(note_info, ('imageList', ...)):
  54. thumbnail_info = traverse_obj(image_info, {
  55. 'height': ('height', {int_or_none}),
  56. 'width': ('width', {int_or_none}),
  57. })
  58. for thumb_url in traverse_obj(image_info, (('urlDefault', 'urlPre'), {url_or_none})):
  59. thumbnails.append({
  60. 'url': thumb_url,
  61. **thumbnail_info,
  62. })
  63. return {
  64. 'id': display_id,
  65. 'formats': formats,
  66. 'thumbnails': thumbnails,
  67. 'title': self._html_search_meta(['og:title'], webpage, default=None),
  68. **traverse_obj(note_info, {
  69. 'title': ('title', {str}),
  70. 'description': ('desc', {str}),
  71. 'tags': ('tagList', ..., 'name', {str}),
  72. 'uploader_id': ('user', 'userId', {str}),
  73. }),
  74. }