久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術文章
文章詳情頁

javascript - 爬取網頁Jquery選擇器first-child的問題

瀏覽:105日期:2023-09-22 16:49:16

問題描述

在爬取一個網站的時候,感覺h2 和 h3 是一樣的結構,為什么 h2:first-child 可以取到數據, h3就不行。

最終的結果h2_1和h2_2是一樣的,沒問題。h3_1是ok的,h3_2是空,請問這是為什么?

代碼如下,

const jsdom = require(’jsdom’);const jquery = require(’jquery’);jsdom.env(’https://www.osram.com/os/news-and-events/spotlights/index.jsp’, [], { defaultEncoding: ’utf-8’}, function(err, window) { if(err) {console.error(’error get news url from page [%s]’);return; } let $ = jquery(window); let el = $(’p.col-xs-6.col-sm-7.colalign:first’); let h2_1 = $(el).find(’h2.font-headline-teaser’).text(); console.log(’h2_1=’ + h2_1); let h2_2 = $(el).find(’h2.font-headline-teaser:first-child’).text(); console.log(’h2_2=’ + h2_2); let h3_1 = $(el).find(’h3.font-sub-headline’).text(); console.log(’h3_1=’ + h3_1); let h3_2 = $(el).find(’h3.font-sub-headline:first-child’).text(); console.log(’h3_2=’ + h3_2); window.close();});

問題解答

回答1:

選擇器xxx:first-child是指,xxx的父元素的第一個子元素為xxx時,選中xxx,需要同時滿足這兩個條件。

不是xxx父元素的第一個子元素,也不是xxx的父元素的子元素中第一個xxx

h2.font-headline-teaser的父元素的第一個子元素為h2.font-headline-teaser,所以能選中

h3.font-sub-headline的父元素的第一個子元素不是h3.font-sub-headline,所以為空

標簽: JavaScript
相關文章:
主站蜘蛛池模板: 岳阳县| 古田县| 丰县| 甘南县| 西平县| 武强县| 曲阳县| 陵水| 东宁县| 芜湖市| 佳木斯市| 南投县| 嘉义市| 泽普县| 页游| 连江县| 益阳市| 滨海县| 保靖县| 鹤峰县| 屏山县| 白玉县| 兴业县| 秭归县| 贵港市| 武夷山市| 库车县| 涞水县| 彩票| 灵璧县| 信丰县| 比如县| 开原市| 罗源县| 石家庄市| 民和| 翁牛特旗| 西平县| 峨边| 鄂州市| 黄平县|