หากไม่มีความกังวลใจเพิ่มเติมฉันจะโพสต์รหัสหลักสำหรับ node.js เพื่อคว้าเนื้อหาของหน้า HTML
รหัสเฉพาะมีดังนี้:
var http = reghed ("http"); var iconv = reghed ('iconv-lite'); var Option = {hostname: "stockdata.stock.hexun.com", เส้นทาง: "/gszl/s601398.shtml"}; var req = http.request (ตัวเลือก, ฟังก์ชั่น (res) {res.on ("data", ฟังก์ชัน (chunk) {console.log (iconv.decode (chunk, "gbk"));});}) on ("ข้อผิดพลาด", ฟังก์ชัน (e) {console.logมาดูเนื้อหาเว็บคลาน nodejs ต่อไปนี้
ฟังก์ชั่น loadpage (url) {var http = ต้องการ ('http'); var pm = สัญญาใหม่ (ฟังก์ชั่น (แก้ไข, ปฏิเสธ) {http.get (url, ฟังก์ชั่น (res) {var html = '' res.on ('data', ฟังก์ชั่น (d) {html += d.tostring () {Resolve (html);});}). on ('ข้อผิดพลาด', ฟังก์ชั่น (e) {ปฏิเสธ (e)});}); return pm;} loadpage ('http://www.baidu.com')