(function (win, doc) { // TODO 客户端内指定页面头部不支持

// 指定日期开启哀悼模式 const MOURN_DATE = []; // 0:全部 1: 首屏 2:指定+首屏 3:指定 const MOURN_MODE = 1;

// i版 const MOURN_INDEX_URL = [ // 主要首屏 '/', '/box-office', '/dashboard', // '/dashboard/movie', // '/dashboard/web-heat', // '/dashboard/tv-viewing', // '/session', // '/calendar', // '/mdb', // '/theater', // '/web-heat', // '/tv-viewing', // //web-heat/*/, // 主要详情页 // '/celebrity', // /^/movie/\d+$/, // /^/companydetail/\d+$/, // /^/tv/\d+$/, // /^/netmovie/\d+$/ ]; // 指定某些页面切入哀悼模式 const MOURN_POINT_URL = [ // //movie/\d+/boxshow/, ]

})(window, document)

python如何爬取到其中 const MOURN_INDEX_URL 的数据内容:要爬取JavaScript代码中的数据,可以使用Python中的正则表达式来匹配需要的数据。根据给出的代码,可以使用以下Python代码来爬取到MOURN_INDEX_URL的数据:

import re

data = '''
(function (win, doc) {
  // TODO 客户端内指定页面头部不支持

  // 指定日期开启哀悼模式
  const MOURN_DATE = [];
  // 0:全部 1: 首屏 2:指定+首屏 3:指定 
  const MOURN_MODE = 1;

  // i版
  const MOURN_INDEX_URL = [
    // 主要首屏
    '/',
    '/box-office',
    '/dashboard',
    // '/dashboard/movie',
    // '/dashboard/web-heat',
    // '/dashboard/tv-viewing',
    // '/session',
    // '/calendar',
    // '/mdb',
    // '/theater',
    // '/web-heat',
    // '/tv-viewing',
    // //web-heat/*/, 
    // 主要详情页
    // '/celebrity',
    // /^/movie/\d+$/,
    // /^/companydetail/\d+$/,
    // /^/tv/\d+$/,
    // /^/netmovie/\d+$/
  ];
  // 指定某些页面切入哀悼模式
  const MOURN_POINT_URL = [
    // //movie/\d+/boxshow/,
  ]

})(window, document)
'''

pattern = r'const MOURN_INDEX_URL = [(.*?)];'
matches = re.search(pattern, data, re.DOTALL)
if matches:
    mourn_index_urls = matches.group(1).split('\n')
    mourn_index_urls = [url.strip() for url in mourn_index_urls if url.strip() and not url.strip().startswith('//')]
    print(mourn_index_urls)

这段代码使用正则表达式匹配const MOURN_INDEX_URL = [...]这一行,并使用re.DOTALL标志来匹配跨行的数据。然后,将匹配到的数据分割成行,并去除空白行和注释行,最后打印出结果。

Python 爬取 JavaScript 代码中的数据 - MOURN_INDEX_URL 示例

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

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