iT邦幫忙

1

用node.JS實作出google搜尋後的頁面總數

如題,
想要使用爬蟲得到如下圖宏框裡面的總數,
https://ithelp.ithome.com.tw/upload/images/20190802/20111873zeaLmykSZi.jpg
已用 request 抓取到google搜尋後的html檔,
但它顯示出來卻沒有對應id範圍的字
https://ithelp.ithome.com.tw/upload/images/20190802/20111873PeQQO9JQ7H.jpg

app.js

const request = require('request')
const url = 'https://www.google.com/search?q=book'
request(url, (err, res, body) => {
  console.log(body)
})

懇請各位大大解答,感謝

p.s. 有查到google-search-results-nodejs,但是有看到pricing,所以沒有使用這個package

ccutmis iT邦高手 2 級 ‧ 2019-08-02 14:59:31 檢舉
<div id="resultStats">約有 9,820,000,000 項結果<nobr> (搜尋時間:0.69 秒) </nobr></div>
======
你把request得到的html檔搜尋看看有沒有 "div id=\"resultStat\""
這搜得到的話表示有東西(你要的就在搜到結果的後面)
搜不到就表示你獲取到的html不是你要的哪一段
我的想法是這樣
Han iT邦研究生 1 級 ‧ 2019-08-02 15:52:47 檢舉
之前也爬過google搜尋後的結果,他們有擋機器人,所以要加header,看看是不是這個問題
p39212053 iT邦新手 4 級 ‧ 2019-08-02 16:19:41 檢舉
@ccutmis 他是回傳一整個檔喔,確定有</html>結尾

@罕罕 不好意思我很菜,可以請問怎麼加header嗎,文章已補充code上去
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

5
dragonH
iT邦超人 5 級 ‧ 2019-08-02 15:11:50
最佳解答

實測過

沒有加 header

user-agent

會拿到不一樣的結果

code

const axios = require('axios');
const cheerio = require('cheerio');

const baseUrl = 'https://www.google.com/search';
const query = {
  ei: 'at5DXeq3H8SSr7wPzc-56AU',
  q: 'it邦幫忙',
  oq: 'it邦幫忙',
  gs_l: 'psy-ab.3..0l9j0i7i30.2043.2043..3196...0.0..0.52.52.1......0....1..gws-wiz.h4Uim--P2-c',
  ved: '0ahUKEwjqlLPEzePjAhVEyYsBHc1nDl0Q4dUDCAo',
  uact: 5
};
const options = {
  headers: {
    'accept-language': ' zh-TW,zh;q=0.9,en-US;q=0.8,en;q=0.7',
    'user-agent': ' Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36',
  }
}

const searchUrl = new URL(baseUrl);
const searchQuery = new URLSearchParams(query);
searchUrl.search = searchQuery;
axios.get(searchUrl.href, options)
  .then((res) => {
    const $ = cheerio.load(res.data);
    console.log($('#resultStats').text());
  })
  .catch(err => console.log(err));

result

約有 1,140,000 項結果 (搜尋時間:0.25 秒)
看更多先前的回應...收起先前的回應...
p39212053 iT邦新手 4 級 ‧ 2019-08-02 16:16:18 檢舉

不好意思,可以稍微解釋一下code嗎 QQ

dragonH iT邦超人 5 級 ‧ 2019-08-02 16:20:22 檢舉

其實沒什麼好解釋的 /images/emoticon/emoticon33.gif

跟你做的事差不多

我的 code 跟你的相比

可能只是多設

user-agent

這個 header

估計你的 code 沒其他問題的話

補上這個 header 應該也能拿到資料

補上 官方 doc 加 header 的範例

const request = require('request');

const options = {
  url: 'https://api.github.com/repos/request/request',
  headers: {
    'User-Agent': 'request'
  }
};

function callback(error, response, body) {
  if (!error && response.statusCode == 200) {
    const info = JSON.parse(body);
    console.log(info.stargazers_count + " Stars");
    console.log(info.forks_count + " Forks");
  }
}

request(options, callback);
p39212053 iT邦新手 4 級 ‧ 2019-08-02 17:00:43 檢舉

抱歉,因為不太懂const query裡面的物件是甚麼意思,
目前是查到ei意思是timestamp,
q是搜尋的字詞,
gs_l是location,
其餘就不太懂了 ....

[原本的code只有使用q]

dragonH iT邦超人 5 級 ‧ 2019-08-02 17:07:48 檢舉

這裡的 query 就是你網址後面的

? xxx = 123&yyy=456

要知道那個代表什麼

可能需要多送幾次不同的搜尋才能推出

p39212053 iT邦新手 4 級 ‧ 2019-08-02 17:13:06 檢舉

好的!
真的非常感謝您的解答(o^^)o

dragonH iT邦超人 5 級 ‧ 2019-08-02 17:16:22 檢舉

/images/emoticon/emoticon12.gif

我要發表回答

立即登入回答