關鍵詞競爭度如何查看,最開始接觸的人應該都知曉,直接去瀏覽器搜索關鍵詞,比如百度搜索某關鍵詞,微博,一行頭部灰色小字,“百度為您找到相關結果約100,000,000個”,這就是關鍵詞的競爭度大小,涉及到到你后期關鍵詞排名優化的難易程度,當然這僅僅是一個參考指標,
當然還有一個很重要的參考指標,關鍵詞的百度指數,這是針對已經收錄的關鍵詞,大部分應該就是研究百度指數的關鍵詞來進行優化處理,大詞都是有指數的!
關鍵點
asyncio --- 異步 I/O
從 Python 3.4 開始,Python 中加入了協程的概念,但這個版本的協程還是以生成器物件為基礎的,在 Python 3.5 則增加了 async/await,使得協程的實作更加方便,
Python 中使用協程最常用的庫莫過于 asyncio
asyncio 是用來撰寫 并發 代碼的庫,使用 async/await 語法,
asyncio 被用作多個提供高性能 Python 異步框架的基礎,包括網路和網站服務,資料庫連接庫,分布式任務佇列等等,
asyncio 往往是構建 IO 密集型和高層級 結構化 網路代碼的最佳選擇,
event_loop:事件回圈,相當于一個無限回圈,我們可以把一些函式注冊到這個事件回圈上,當滿足條件發生的時候,就會呼叫對應的處理方法,
coroutine:中文翻譯叫協程,在 Python 中常指代為協程物件型別,我們可以將協程物件注冊到時間回圈中,它會被事件回圈呼叫,我們可以使用 async 關鍵字來定義一個方法,這個方法在呼叫時不會立即被執行,而是回傳一個協程物件,
task:任務,它是對協程物件的進一步封裝,包含了任務的各個狀態,
future:代表將來執行或沒有執行的任務的結果,實際上和 task 沒有本質區別,
async/await 關鍵字,是從 Python 3.5 才出現的,專門用于定義協程,其中,async 定義一個協程,await 用來掛起阻塞方法的執行,
asyncio gather和wait并發方式
gather 比 wait 更加高層,
gather 可以將任務分組,一般優先使用 gather,
在某些定制化任務需求的時候,會使用 wait,
單執行緒
#百度搜索結果數(競爭度大小)抓取
# 20201113@author:WX:huguo00289
# -*- coding=utf-8 -*-
import requests,re,time
from fake_useragent import UserAgent
def search(keyword):
sum=''
ua=UserAgent()
url=f'https://www.baidu.com/s?wd={keyword}&ie=UTF-8'
headers= {
'User-Agent':ua.random,
'Cookie':'BIDUPSID=E8605F17778754AD6BAA328A17329DAF; PSTM=1595994013; BAIDUID=E8605F17778754AD8EAC311EDCEC5A37:FG=1; BD_UPN=12314353; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; COOKIE_SESSION=75158_0_8_0_82_8_0_0_0_8_1_0_75159_0_1_0_1605083022_0_1605083023%7C9%230_0_1605083023%7C1; H_PS_645EC=c097mGOFZEl3IZjKw2lVOhIl4YyhcIr2Zp3YMimT2D62xwJo8q%2B9jeQnZq3gvUXMGbhD; BA_HECTOR=a42l8ka5ah8h0003611fqs8b60p; BD_HOME=1; H_PS_PSSID=32818_1452_33045_32939_33060_32973_32705_32961',
}
try:
html=requests.get(url,headers=headers,timeout=5).content.decode('utf-8')
#time.sleep(1)
sum=re.search(r'<span class="nums_text">百度為您找到相關結果約(.+?)個</span>',html,re.M|re.I).group(1)
except Exception as e:
print(f"錯誤代碼: {e}")
if sum !='':
print(keyword,sum)
def main():
keywords=["seo優化技巧","百度站長平臺","sem怎么學習","全網推廣營銷","seo網站優化方案","百度燒錢推廣","自媒體推廣策劃"]
for keyword in keywords:
search(keyword)
print('共運行了{}秒'.format(end - start)) # 程式耗時
asyncio+aiohttp 異步-wait
async def get_content(keyword):
ua = UserAgent()
headers = {
'User-Agent': ua.random,
'Cookie': 'BIDUPSID=E8605F17778754AD6BAA328A17329DAF; PSTM=1595994013; BAIDUID=E8605F17778754AD8EAC311EDCEC5A37:FG=1; BD_UPN=12314353; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; COOKIE_SESSION=75158_0_8_0_82_8_0_0_0_8_1_0_75159_0_1_0_1605083022_0_1605083023%7C9%230_0_1605083023%7C1; H_PS_645EC=c097mGOFZEl3IZjKw2lVOhIl4YyhcIr2Zp3YMimT2D62xwJo8q%2B9jeQnZq3gvUXMGbhD; BA_HECTOR=a42l8ka5ah8h0003611fqs8b60p; BD_HOME=1; H_PS_PSSID=32818_1452_33045_32939_33060_32973_32705_32961',
}
async with aiohttp.ClientSession() as session:
response = await session.get(f'https://www.baidu.com/s?wd={keyword}&ie=UTF-8',headers=headers,timeout=5)
content = await response.read()
return content
async def get_num(keyword):
sum=''
content = await get_content(keyword)
try:
html=content.decode('utf-8')
#time.sleep(1)
sum=re.search(r'<span class="nums_text">百度為您找到相關結果約(.+?)個</span>',html,re.M|re.I).group(1)
except Exception as e:
print(f"錯誤代碼: {e}")
if sum !='':
print(keyword,sum)
def run():
tasks = []
start = time.time() # 記錄起始時間戳
keywords=["seo優化技巧","百度站長平臺","sem怎么學習","全網推廣營銷","seo網站優化方案","百度燒錢推廣","自媒體推廣策劃"]
loop = asyncio.get_event_loop()
for keyword in keywords:
c = get_num(keyword)
# 通過回傳的協程物件進一步封裝成一個任務物件
task = asyncio.ensure_future(c)
tasks.append(task)
loop.run_until_complete(asyncio.wait(tasks))
end = time.time() # 獲取結束時間戳
print('共運行了{}秒'.format(end - start)) # 程式耗時
asyncio+aiohttp 異步-gather
def run_gather():
start = time.time() # 記錄起始時間戳
keywords=["seo優化技巧","百度站長平臺","sem怎么學習","全網推廣營銷","seo網站優化方案","百度燒錢推廣","自媒體推廣策劃"]
tasks = [asyncio.ensure_future(get_num(keyword)) for keyword in keywords]
loop = asyncio.get_event_loop()
tasks = asyncio.gather(*tasks)
loop.run_until_complete(tasks)
end = time.time() # 獲取結束時間戳
print('共運行了{}秒'.format(end - start)) # 程式耗時
完整demo請關注本渣渣公眾號:二爺記
后臺回復關鍵詞:異步爬蟲
獲取py檔案
參考來源
[1] asyncio --- 異步 I/O — Python 3.9.0 檔案
[2] asyncio+aiohttp異步爬蟲
[3] Python爬蟲學習筆記 asyncio+aiohttp 異步爬蟲原理和決議
[4] 從0到1,Python異步編程的演進之路
[5] asyncio gather和wait并發方式
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/219007.html
標籤:其他
上一篇:Odoo已知Bug
下一篇:Python 實作文字聊天室
