python打造seo必备工具-自动查询排名

    因为工作需要,利用业余时间开发的,可以查询百度排名+360排名工具,附上代码。

  

#360搜索排名查询
# -*- coding=utf-8 -*-
import requests
from lxml import etree
 
headers={
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36'
}
 
def ss360(keyword,num,search_url):
    search_datas=''
    for x in range(1,num+1):
        print(f"正在查询第{x}页搜索结果...")
        url=f"https://www.so.com/s?q={keyword}&pn={x}"
        html=requests.get(url,headers=headers).text
        #print(html)
        con=etree.HTML(html)
        #print(con)
        title=con.xpath('//h3[@class="res-title "]/a/text()')
        #print(title)
        #print(len(title))
 
        datas=con.xpath('///h3/a')
        #print(len(datas))
        for data in datas:
            #print(data)
            try:
                data_res=data.attrib['data-res']
                #pos=re.findall('"pos":(.+?),',data_res,re.S)
                #print(pos[0])
                data_res=eval(data_res) #转换为字典数据
                pos=data_res['pos']
                print(pos)
            except:
                pos=''
            try:
                data_url=data.attrib['data-url']
            except:
                data_url=data.attrib['href']
            if "http://e.360.cn/static/" not in data_url and "javascript:" not in data_url:
                print(data_url)
                print('\r')
            if search_url in data_url:
                pm=(x-1)*10+pos
                print(f'第{x}页,排名:{pos}/{pm},链接:{data_url}')
                search_data=f'第{x}页,排名:{pos}/{pm},链接:{data_url}'
                search_datas ='%s%s%s'%(search_datas,search_data,'\n')
    print(search_datas)
    return search_datas
 
if __name__ == "__main__":
    search_datas = ''
    keyword = input('请输入关键词>>')
    num = int(input('请输入最大查询页数>>'))
    search_website = input('请输入网址(建议输入不带www网址)>>')
    search_datas=ss360(keyword,num,search_website)
    print('========================查询结果========================\n\n')
    print(search_datas)
    print('\n\n========================查询结束========================\n')

  

转载于:https://www.cnblogs.com/68xi/p/11206597.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值