html状态查询爱站,批量获取爱站数据
批量獲取愛站數(shù)據(jù)
SEO工作中經(jīng)常需要批量獲取愛站的一些數(shù)據(jù),而實際操作中這方面的工具又很少,現(xiàn)在共享一個簡單又實用的開源python抓取Code。
首先你需要保存下面的代碼,后綴為“.py”即可,比如“aizhan.py”,第二步,就是處理你的需求,把需要抓取的網(wǎng)址,列一列保存為一個url.txt的文件,同aizhan.py文件放到同一個文件夾下。第三步就是最終解決問題了,打開終端進入到當前文件夾下,輸入python aizhan.py即可在屏幕看到數(shù)據(jù)依次的被抓去下來,保存的文件叫weight.csv。需要強調(diào)的是,最好找臺Mac蘋果筆記本運行該代碼,因為windows需要安裝python環(huán)境變量,顯得不值當。目前能夠抓取的數(shù)據(jù)包括網(wǎng)址的PR、PC來路最大值、移動來路最大值、PC關鍵詞數(shù)量、移動關鍵詞數(shù)量。
#復制以下代碼:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import requests
import re
import json
import time
def get_url():
''' 從 url.txt 中獲取要查詢的 url 地址 '''
with open('url.txt', 'r') as f:
for line in f.xreadlines():
yield line
def get_html(url):
''' 獲取頁面 html 內(nèi)容 '''
url = 'http://baidurank.aizhan.com/baidu/{url}/'.format(url=url)
r = requests.get(url)
if r.status_code == 200:
return r.text
def get_from(html):
''' 獲取來路 '''
p = '\d+ ~ (\d+)IP'
m = re.findall(p, html)
if m:
ret = m[0]
else:
ret = None
return ret
def get_total(html, domain):
''' 獲取總收錄數(shù) '''
p = "url:'http://www.aizhan.com/ajaxAction/shoulu3.php',.*?success"
m = re.findall(p, html, re.S)
if m:
html = m[0]
p_ = "data:{.*?cc:'(.*?)',rn:'(.*?)'},"
m_ = re.findall(p_, html)
if m_:
cc = m_[0][0]
rn = m_[0][1]
data = dict(domain=domain, cc=cc, rn=rn)
url = 'http://www.aizhan.com/ajaxAction/shoulu3.php?domain={domain}&cc={cc}&rn={rn}'.format(**data)
r = requests.get(url)
if r.status_code == 200:
return json.loads(r.text).get('baidu_0days', 0)
def get_word_count(html, domain):
''' 獲取總詞數(shù) '''
p = 'url:"http://baidurank.aizhan.com/api/wordlocation",.*?success'
m = re.findall(p, html, re.S)
pcwl = None
mwl = None
if m:
html = m[0]
p_ = "data:{.*?ajaxKey:'(.*?)',rn:'(.*?)'},"
m_ = re.findall(p_, html)
if m_:
ajaxKey = m_[0][0]
rn = m_[0][1]
data = dict(domain=domain, ajaxKey=ajaxKey, rn=rn)
url = 'http://baidurank.aizhan.com/api/wordlocation?domain={domain}&ajaxKey={ajaxKey}&rn={rn}'.format(**data)
r = requests.get(url)
if r.status_code == 200:
data = json.loads(r.text)
pcwl = data.get('pcWL')[0]
mwl = data.get('mWL')[0]
return pcwl, mwl
def get_data(html, url):
''' 獲取需要的 html 塊 '''
p = '
(.*?)(.*?)'m = re.findall(p, html, re.S)
pc = None
mobile = None
title = None
for o in m:
if len(o) == 2:
title = o[0].encode('utf-8')
if title == '(PC端)來路':
pc = get_from(o[1])
elif title == '(移動)來路':
mobile = get_from(o[1])
total = get_total(html, url)
if total:
total = total.replace(',', '')
pcwl, mwl = get_word_count(html, url)
return dict(url=url, pc=pc, mobile=mobile, total=total, pcwl=pcwl, mwl=mwl)
def main():
with open('weight.csv', 'w+') as f:
f.write('url pc 來路,移動來路,總收錄,pc 總詞數(shù),移動總詞數(shù)\n')
for url in get_url():
url = url.replace('\n', '').replace('\r', '')
html = get_html(url)
s = '{url},{pc},{mobile},{total},{pcwl},{mwl}\n'
if html:
print s.format(**get_data(html, url))
f.write(s.format(**get_data(html, url)))
time.sleep(5)
if __name__ == '__main__':
main()
總結(jié)
以上是生活随笔為你收集整理的html状态查询爱站,批量获取爱站数据的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: S12岩雀新版本天赋怎么点 大乱斗出装怎
- 下一篇: 自动化html定位,Selenium自动