欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python獲取百度熱榜鏈接的實例方法

 更新時間:2020年08月25日 07:52:14   作者:FXL  
在本篇文章里小編給大家整理了關(guān)于python獲取百度熱榜鏈接的實例方法,需要的朋友們可以學(xué)習(xí)參考下。

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

https://www.baidu.com/

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

86e9a05d577dce21c9942ae01e42f57.png

鏈接分析:

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

3fbc8821b4472585b2a4054f8a22b19.png

完整代碼:

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)

你會發(fā)現(xiàn)結(jié)果很長:

5a4ab5594dcdd44ecffd034fa2af3af.png

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

e7b4deebe2f8dde511791804ddbc3be.png

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

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

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

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

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

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

pn是頁碼

from lxml import etree
import re
import requests
import string
import json
headers = {
  "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.text
html = 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獲取百度熱榜鏈接的實例方法的文章就介紹到這了,更多相關(guān)教你用python獲取百度熱榜鏈接內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論