从节点js中的html响应中获取值

非常新的 nodejs 没有找到类似的帖子。我正在尝试data-clp-course-id从下面的代码中获取 html 响应,


const https = require('https')

  

const options = {

    hostname: 'www.udemy.com',

    path: '/course/az-303-microsoft-azure-architect-practice-test-2020/',

    method: 'GET'

  }

  

  const req = https.request(options, res => {

    console.log(`statusCode: ${res.statusCode}`)

  

    res.on('data', d => {

     // process.stdout.write(d)

    })

  })

  

  req.on('error', error => {

    console.error(error)

  })

  

  req.end()

示例 html 响应(仅给出响应的一部分):


<!DOCTYPE html>

<html>


<head>

    <meta name="medium" content="mult">

    <meta name="title" content="AZ-303: Microsoft Azure Architect Practice Test 2020">

    <meta property="fb:app_id" content="313137469260">

    <meta http-equiv="X-UA-Compatible" content="IE=Edge">

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <meta property="udemy_com:available" content="1">

    <meta property="udemy_com:category" content="IT &amp; Software">

    <meta property="udemy_com:instructor" content="https://www.udemy.com/user/fariha-rubab2/">

    <meta property="udemy_com:price" content="₹1,280">

    <meta property="og:title" content="AZ-303: Microsoft Azure Architect Practice Test 2020">

    <meta property="og:url" content="https://www.udemy.com/course/az-303-microsoft-azure-architect-practice-test-2020/">

    <script>

对如何data-clp-course-id从 html 响应中获取值有帮助吗?


慕斯王
浏览 104回答 1
1回答

慕少森

只是使用迈克的建议async function getCourseCode() {&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; let response = await fetch('https://www.udemy.com/course/az-303-microsoft-azure-architect-practice-test-2020/');&nbsp; &nbsp; &nbsp; &nbsp; let body = await response.text();&nbsp; &nbsp; &nbsp; &nbsp; console.log(response.status);&nbsp; &nbsp; &nbsp; &nbsp; //console.log(body);&nbsp; &nbsp; &nbsp; &nbsp; let responseBody = body.match(/data-clp-course-id="(\d+)"/)&nbsp; &nbsp; &nbsp; &nbsp; let courseID = responseBody[1];&nbsp; &nbsp; &nbsp; &nbsp; console.log(courseID);&nbsp; &nbsp; }&nbsp; &nbsp; catch(exception){&nbsp; &nbsp; &nbsp; &nbsp; console.log(exception);&nbsp; &nbsp; }}getCourseCode();
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript