溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

怎么用python獲取百度熱榜鏈接

發(fā)布時(shí)間:2020-08-25 09:20:43 來(lái)源:億速云 閱讀:151 作者:Leah 欄目:編程語(yǔ)言

這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)?lái)有關(guān)怎么用python獲取百度熱榜鏈接,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

目標(biāo)網(wǎng)址:

https://www.baidu.com/

要獲取的內(nèi)容:

怎么用python獲取百度熱榜鏈接

鏈接分析:

從下圖可以看出只需要獲取關(guān)鍵字,再構(gòu)建就可以了。

怎么用python獲取百度熱榜鏈接

完整代碼:

import requests
import pprint
import re
import urllib.parse

url = 'https://www.baidu.com/'

headers = {
    'Host': 'www.baidu.com',
    'Referer': 'https://www.baidu.com/',
    'User-Agent': 你的User-Agent,
    'Cookie': 你的Cookie
}

response = requests.get(url, headers=headers).content.decode('utf-8')
# 獲取關(guān)鍵字
pat = '"pure_title": "(.*?)"'
keyword = re.findall(pat, response, re.S)
print(len(keyword))

for hot_word in keyword:
    # 漢字不符合url標(biāo)準(zhǔn),所以這里需要進(jìn)行url編碼
    i = urllib.parse.quote(hot_word, encoding='utf-8', errors='replace')
    # url構(gòu)建
    link = f'https://www.baidu.com/s?cl=3&tn=baidutop10&fr=top1000&wd={i}&rsv_idx=2&rsv_dl=fyb_n_homepage&hisfilter=1'
    print(link)

你會(huì)發(fā)現(xiàn)結(jié)果很長(zhǎng):

怎么用python獲取百度熱榜鏈接

但其實(shí)關(guān)鍵字后面的幾個(gè)參數(shù)是可以去掉的,這樣url就沒有那么長(zhǎng)了。

怎么用python獲取百度熱榜鏈接

上述就是小編為大家分享的怎么用python獲取百度熱榜鏈接了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI