Javascript 为什么可以';我似乎使用HTML文档来构造jsdom对象?

Javascript 为什么可以';我似乎使用HTML文档来构造jsdom对象?,javascript,node.js,http,jsdom,Javascript,Node.js,Http,Jsdom,目标: 通过标签从外部网页提取值 方法: 执行HTTP请求并用响应构造一个jsdom对象。使用jsdom的查询选择器从标记中获取值 问题: 当我尝试访问任何标记的值时控制台.log(dom.window.document.querySelector(“h4”).textContent)。。。我得到一个错误:“无法读取null的属性'textContent' 这一定意味着由于chunk参数(chunk是响应对象的字符串)存在问题,因此未正确构造jsdom对象 讨论: 我的猜测是,响应块中的引号转义

目标:
通过标签从外部网页提取值

方法:
执行HTTP请求并用响应构造一个
jsdom
对象。使用
jsdom
的查询选择器从标记中获取值

问题:
当我尝试访问任何标记的值时<代码>控制台.log(dom.window.document.querySelector(“h4”).textContent)。。。我得到一个错误:“无法读取null的属性'textContent'

这一定意味着由于
chunk
参数(chunk是响应对象的字符串)存在问题,因此未正确构造
jsdom
对象

讨论:
我的猜测是,响应块中的引号转义有问题,但我的正则表达式尝试没有看到任何结果。
dom.window.document.querySelector(“h4”).textContent
如果我通过一个简单的字符串,比如
测试,它就可以正常工作

代码的重要部分:

res.on('data', (chunk) => {
    console.log(typeof(chunk)); // string
    const dom = new JSDOM(chunk);
    console.log(dom.window.document.querySelector("h4").textContent);

  });
所有代码:

var querystring = require('querystring');
var http = require('http');
const jsdom = require("jsdom");
const { JSDOM } = jsdom;

const postData = querystring.stringify({
  'id': '1'
});

const options = {
  hostname: 'www.southernnbtruckers.ca',
  port: 80,
  path: '/search/info/6',
  method: 'POST',
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded',
    'Content-Length': Buffer.byteLength(postData)
  }
};

const req = http.request(options, (res) => {
  res.setEncoding('utf8');

  //Problem is likely to do with the HTTP response (chunk)
  res.on('data', (chunk) => {
    console.log(typeof(chunk)); // string
    const dom = new JSDOM(chunk);
    console.log(dom.window.document.querySelector("h4").textContent); //Cannot read property 'textContent' of null

  });
  res.on('end', () => {
      //Do stuff
  });
});

req.on('error', (e) => {
  console.error(`problem with request: ${e.message}`);
});

req.write(postData);
req.end();  
<html>
<head>
    <base href="http://www.southernnbtruckers.ca/">
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
    <meta name="generator" content="People and Groups"/>
    <meta name="keywords" content=""/>
    <title>TRUCKERS - Search</title>
    <link rel="stylesheet" type="text/css" href="/core/styles/style_custom.php?org_name=truckers&language=english"/>
</head>
<body><a name="top"></a>
<div id="Container">
    <div id="Header">
        <table id="Lang">
            <tr>
                <td valign="middle">&nbsp;&nbsp;&nbsp;<a href="/login">Login</a></td>
            </tr>
        </table>
    </div>
    <div id="MainNav">
        <div id="Nav">
            <ul>
                <li class="LeftSelectedNav"><a href="/search">Home</a></li>
                <li><a href="/contact_information">Contact</a></li>
                <li style="float:right;" class="right">&nbsp;</li>
            </ul>
        </div>
    </div>
    <div id="MainContent">
        <div id="SideNav">
            <div id="Sub">
                <ul>
                    <li id="SubTitle">Home</li>
                    <li class="subsub"><a href="/our_mission_statemnt">Our Mission Statement</a
                    ></li>
                    <li class="subsub"><a href="/our_executive">Our Executive</a></li>
                    <li class="currentSub"><a href="/search">Search</a></li>
                    <li id="SubSpacer"></li>
                </ul>
                <ul>
                    <li class="SubBlank"><h4>PO Box 342, Harvey, York Co. NB E6K 3W9</h4>
                        <center>
                            <table class="featureImageTable">
                                <tr>
                                    <td><img src="/uploads/Website_Assets/truckers-sidetest.jpg" alt="Side Test"
                                             title=""/></td>
                                </tr>
                                <tr>
                                    <td></td>
                                </tr>
                            </table>
                        </center>
                        <br/><br/><a href="http://www.partsfortrucks.com" target="_tab">
                            <center>
                                <table class="featureImageTable">
                                    <tr>
                                        <td><img src="/uploads/Website_Assets/PartTrucks.jpg" alt="Parts Trucks 300px"
                                                 title=""/></td>
                                    </tr>
                                    <tr>
                                        <td></td>
                                    </tr>
                                </table>
                            </center>
                        </a></p></li>
                    <li id="SubSpacer"></li>
                </ul>
            </div>
        </div>
        <div id="Main">
            <table class="data" id="mainContentTable" cellspacing="0" cellpadding="0" width="100%">
                <tr>
                    <td valign="top"><h1>Truckers Search</h1>Find what you need! This database is easy to use - if
                        you're looking for a specific piece of equipment for hire just use the pull down menu that says
                        "Company Name" and locate the equipment you require, then press return or the filter button. If
                        you're looking for a company to work in a specific county in New Brunswick - just use the pull
                        down menu to identify the county. You can also click on the name of any trucker to bring up
                        their equipment profile and contact information.
                        <hr/>
                        <a href="/search">&lt;&lt; Back to search</a>
                        <hr>
                        <h1>Gary MacBean</h1>
                        <div class="contact">Contact: Gary MacBean</div>
                        <hr>
                        Address1:&nbsp;150 Sunrise Estates Avenue<br>City:&nbsp;New Maryland<br>Province:&nbsp;NB<br>Postal
                        Code:&nbsp;E3C 1G6<br>Phone:&nbsp;1 506 459-3609<br>Cell Phone:&nbsp;1 506 444-1358<br>Fax:&nbsp;1
                        506 459-5154<br>
                        <hr>
                        Number Of Trucks:&nbsp;2<br>Has Dump Trailer:&nbsp;Yes<br>Has Tandem Dump Truck:&nbsp;Yes<br>Has
                        Belly Dump:&nbsp;Yes<br>Has Asphalt Tarp Spreader:&nbsp;Yes<br>
                        <hr>
                        Has Compensation WorkSafeNB:&nbsp;Yes<br>Has Liability Insurance:&nbsp;Yes<br>Has HST Number:&nbsp;Yes<br>
                        <hr>
                        Works Province Wide:&nbsp;Yes<br>
                        <hr>
                        <hr/>
                        <a href="/contact_information">Comment, Questions?</a></td>
                </tr>
            </table>
            <div id="Footer">
                <hr/>
                <p>
                <h2>Serving Central and Southern New Brunswick</h2><br/><br/>Powered by: <a
                    href="http://www.peopleandgroups.com" title="www.peopleandgroups.com">People&Groups</a></p></div>
        </div>
    </div>
</div>
</body>
</html>
供参考的HTML代码:

var querystring = require('querystring');
var http = require('http');
const jsdom = require("jsdom");
const { JSDOM } = jsdom;

const postData = querystring.stringify({
  'id': '1'
});

const options = {
  hostname: 'www.southernnbtruckers.ca',
  port: 80,
  path: '/search/info/6',
  method: 'POST',
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded',
    'Content-Length': Buffer.byteLength(postData)
  }
};

const req = http.request(options, (res) => {
  res.setEncoding('utf8');

  //Problem is likely to do with the HTTP response (chunk)
  res.on('data', (chunk) => {
    console.log(typeof(chunk)); // string
    const dom = new JSDOM(chunk);
    console.log(dom.window.document.querySelector("h4").textContent); //Cannot read property 'textContent' of null

  });
  res.on('end', () => {
      //Do stuff
  });
});

req.on('error', (e) => {
  console.error(`problem with request: ${e.message}`);
});

req.write(postData);
req.end();  
<html>
<head>
    <base href="http://www.southernnbtruckers.ca/">
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
    <meta name="generator" content="People and Groups"/>
    <meta name="keywords" content=""/>
    <title>TRUCKERS - Search</title>
    <link rel="stylesheet" type="text/css" href="/core/styles/style_custom.php?org_name=truckers&language=english"/>
</head>
<body><a name="top"></a>
<div id="Container">
    <div id="Header">
        <table id="Lang">
            <tr>
                <td valign="middle">&nbsp;&nbsp;&nbsp;<a href="/login">Login</a></td>
            </tr>
        </table>
    </div>
    <div id="MainNav">
        <div id="Nav">
            <ul>
                <li class="LeftSelectedNav"><a href="/search">Home</a></li>
                <li><a href="/contact_information">Contact</a></li>
                <li style="float:right;" class="right">&nbsp;</li>
            </ul>
        </div>
    </div>
    <div id="MainContent">
        <div id="SideNav">
            <div id="Sub">
                <ul>
                    <li id="SubTitle">Home</li>
                    <li class="subsub"><a href="/our_mission_statemnt">Our Mission Statement</a
                    ></li>
                    <li class="subsub"><a href="/our_executive">Our Executive</a></li>
                    <li class="currentSub"><a href="/search">Search</a></li>
                    <li id="SubSpacer"></li>
                </ul>
                <ul>
                    <li class="SubBlank"><h4>PO Box 342, Harvey, York Co. NB E6K 3W9</h4>
                        <center>
                            <table class="featureImageTable">
                                <tr>
                                    <td><img src="/uploads/Website_Assets/truckers-sidetest.jpg" alt="Side Test"
                                             title=""/></td>
                                </tr>
                                <tr>
                                    <td></td>
                                </tr>
                            </table>
                        </center>
                        <br/><br/><a href="http://www.partsfortrucks.com" target="_tab">
                            <center>
                                <table class="featureImageTable">
                                    <tr>
                                        <td><img src="/uploads/Website_Assets/PartTrucks.jpg" alt="Parts Trucks 300px"
                                                 title=""/></td>
                                    </tr>
                                    <tr>
                                        <td></td>
                                    </tr>
                                </table>
                            </center>
                        </a></p></li>
                    <li id="SubSpacer"></li>
                </ul>
            </div>
        </div>
        <div id="Main">
            <table class="data" id="mainContentTable" cellspacing="0" cellpadding="0" width="100%">
                <tr>
                    <td valign="top"><h1>Truckers Search</h1>Find what you need! This database is easy to use - if
                        you're looking for a specific piece of equipment for hire just use the pull down menu that says
                        "Company Name" and locate the equipment you require, then press return or the filter button. If
                        you're looking for a company to work in a specific county in New Brunswick - just use the pull
                        down menu to identify the county. You can also click on the name of any trucker to bring up
                        their equipment profile and contact information.
                        <hr/>
                        <a href="/search">&lt;&lt; Back to search</a>
                        <hr>
                        <h1>Gary MacBean</h1>
                        <div class="contact">Contact: Gary MacBean</div>
                        <hr>
                        Address1:&nbsp;150 Sunrise Estates Avenue<br>City:&nbsp;New Maryland<br>Province:&nbsp;NB<br>Postal
                        Code:&nbsp;E3C 1G6<br>Phone:&nbsp;1 506 459-3609<br>Cell Phone:&nbsp;1 506 444-1358<br>Fax:&nbsp;1
                        506 459-5154<br>
                        <hr>
                        Number Of Trucks:&nbsp;2<br>Has Dump Trailer:&nbsp;Yes<br>Has Tandem Dump Truck:&nbsp;Yes<br>Has
                        Belly Dump:&nbsp;Yes<br>Has Asphalt Tarp Spreader:&nbsp;Yes<br>
                        <hr>
                        Has Compensation WorkSafeNB:&nbsp;Yes<br>Has Liability Insurance:&nbsp;Yes<br>Has HST Number:&nbsp;Yes<br>
                        <hr>
                        Works Province Wide:&nbsp;Yes<br>
                        <hr>
                        <hr/>
                        <a href="/contact_information">Comment, Questions?</a></td>
                </tr>
            </table>
            <div id="Footer">
                <hr/>
                <p>
                <h2>Serving Central and Southern New Brunswick</h2><br/><br/>Powered by: <a
                    href="http://www.peopleandgroups.com" title="www.peopleandgroups.com">People&Groups</a></p></div>
        </div>
    </div>
</div>
</body>
</html>

卡车司机-搜索
    主页
    纽约州哈维市邮政信箱342号,NB E6K 3W9

卡车司机会找到你需要的东西!此数据库易于使用-如果 你正在寻找一个特定的设备租用只需使用下拉菜单说 “公司名称”并找到您需要的设备,然后按return或filter按钮。如果 你正在寻找一家在新不伦瑞克省某个特定县工作的公司——只需使用pull 下拉菜单以识别县。你也可以点击任何一个卡车司机的名字 他们的设备概况和联系方式。

加里·麦克宾 联系人:加里·麦克宾
地址1:150 Sunrise Estates Avenue市:新马里兰州省:NB邮政 代码:E3C1G6
电话:1506459-3609
手机:1506444-1358
传真:1 506 459-5154

卡车数量:2辆
有自卸拖车:是
有串联自卸卡车:是
有 肚皮堆:是
有沥青油布撒布机:是

是否有赔偿工作?是否有责任保险?是否有HST号码?是否
在全省范围内工作:是



服务于新不伦瑞克中部和南部

由以下设备供电:


想法?我是否应该使用不同的方法从其他站点捕获数据?

您的代码假设您将在一个“块”中获取所有HTML,但情况并非如此。您必须将这些片段累积到一个大字符串中,并且只在DOM完成后才尝试构建DOM。谢谢!聪明,因此有了“块”这个词。我想我将把响应连接成一个字符串,并在
res.on('end',()=>…
中使用它们。您的代码假设您将在一个“块”中获得所有HTML,但事实并非如此。您必须将这些片段累积成一个大字符串,并且只有在完成后才尝试构建DOM。谢谢!太棒了,因此有了“chunk”这个词。我想我会将响应连接成一个字符串,并在
res.on('end',()=>…
中使用它们。