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

您的位置:首頁技術(shù)文章
文章詳情頁

python獲取百度熱榜鏈接的實(shí)例方法

瀏覽:2日期:2022-07-13 10:27:02

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

https://www.baidu.com/

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

python獲取百度熱榜鏈接的實(shí)例方法

鏈接分析:

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

python獲取百度熱榜鏈接的實(shí)例方法

完整代碼:

import requestsimport pprintimport reimport urllib.parseurl = ’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é)果很長:

python獲取百度熱榜鏈接的實(shí)例方法

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

python獲取百度熱榜鏈接的實(shí)例方法

內(nèi)容擴(kuò)展:

python 爬取簡單的百度搜索結(jié)果

爬取百度搜索結(jié)果

主要還要借助xpath helper谷歌瀏覽器的插件來操作更容易找到需要查找信息的xpath位置

還要首先了解一下百度搜索請求的參數(shù) lm默認(rèn)為0,天數(shù)限制,但是好像只有1有用。

默認(rèn)每頁10條信息,rn

pn是頁碼

from lxml import etreeimport reimport requestsimport stringimport jsonheaders = { 'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36'}response = requests.get(’https://www.baidu.com/s?wd=騰訊視頻優(yōu)惠&lm=1’,headers=headers)r = response.texthtml = etree.HTML(r,etree.HTMLParser())r1 = html.xpath(’//h3’)r2 = html.xpath(’//*[@class='c-abstract']’)r3 = html.xpath(’//a[@class='c-showurl']’)for i in range(10) : r11 = r1[i].xpath(’string(.)’) r22 = r2[i].xpath(’string(.)’) r33 = r3[i].xpath(’string(.)’) # with open(’test.txt’, ’a’, encoding=’utf-8’) as f: # f.write(json.dumps(r11,ensure_ascii=False) + ’n’) # f.write(json.dumps(r22, ensure_ascii=False) + ’n’) # f.write(json.dumps(r33, ensure_ascii=False) + ’n’) print(r11,end=’n’) print(r22,end=’n’) print(r33) print()

到此這篇關(guān)于python獲取百度熱榜鏈接的實(shí)例方法的文章就介紹到這了,更多相關(guān)教你用python獲取百度熱榜鏈接內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: 百度 Python
相關(guān)文章:
主站蜘蛛池模板: 汉中市| 绍兴县| 新龙县| 晋宁县| 合水县| 榆树市| 台州市| 宜阳县| 札达县| 新乡县| 大安市| 自治县| 德钦县| 奇台县| 汾西县| 嘉荫县| 凤庆县| 五大连池市| 藁城市| 滦平县| 楚雄市| 潮州市| 绥芬河市| 邵武市| 攀枝花市| 西丰县| 白河县| 大余县| 光山县| 治县。| 巴林左旗| 松滋市| 乃东县| 罗江县| 连州市| 芮城县| 玉林市| 紫金县| 临猗县| 石城县| 白银市|