最新动态
python关键词排名_Python百度seo排名查询,关键词排名查询优化版
2024-10-30 13:50

zsurl = f'https://www.baidu.com{baidu_url}'

python关键词排名_Python百度seo排名查询,关键词排名查询优化版

# 解析真实地址

else:

r = requests.head(baidu_url, stream=True)

website_url = r.headers['Location']

#print(website_url)

return website_url

#获取搜索数据

def get_search_data(search_objects,search_website):

seach_data=''

baidu_idze1 = r'id="([0-9]{1,4})" srcid'

baidu_idze2 = r'id="([0-9]{1,4})" tpl'

titleze = r'target="_blank"(.+?)'

baidu_urlze1 = r'href = "(.+?)"'

baidu_urlze2 = r'href="(.+?)"'

for search_object in search_objects:

# 获取排名

try:

baidu_id = re.findall(baidu_idze1, search_object, re.S)

baidu_id= baidu_id[0]

except:

baidu_id = re.findall(baidu_idze2, search_object, re.S)

baidu_id = baidu_id[0]

#print(baidu_id)

# 获取标题

title = re.findall(titleze,search_object,re.S)

title = title[0]

# 去除多余的字符

title = re.sub(r'[ > ]', "", title)

#print(title)

# 获取链接

try:

baidu_url= re.findall(baidu_urlze1,search_object,re.S)

baidu_url= baidu_url[0]

except:

baidu_url= re.findall(baidu_urlze2,search_object,re.S)

baidu_url= baidu_url[0]

#print(baidu_url)

website_url=get_website_url(baidu_url)

#print(website_url)

if search_website in website_url:

print(f'关键词:{keyword},排名:{baidu_id},标题:{title},网址:{website_url},百度链接:{baidu_url}')

search_result= f'关键词:{keyword},排名:{baidu_id},标题:{title},网址:{website_url},百度链接:{baidu_url}'

seach_data = '%s%s%s' % (seach_data, search_result, ' ')

time.sleep(3) # 设置延迟间隔

return seach_data

if __name__ == '__main__':

search_datas=''

i=0

keyword = input('请输入关键词>>')

num = int(input('请输入最大查询页数>>'))

search_website = input('请输入网址(建议输入不带www网址)>>')

search_urls=get_search_url(keyword,num)

for search_url in search_urls:

print(f'正在查询{i + 1}页排名...')

search_objects=get_search_object(search_url)

#print(search_objects)

search_data=get_search_data(search_objects, search_website)

#print(search_data)

i=i+1

search_datas=search_datas+search_data

print('========================查询结果======================== ')

print(search_datas)

print(' ========================查询结束======================== ')

#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(' ')

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,' ')

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)

    以上就是本篇文章【python关键词排名_Python百度seo排名查询,关键词排名查询优化版】的全部内容了,欢迎阅览 ! 文章地址:http://fabua.ksxb.net/quote/7346.html 
     行业      资讯      企业新闻      行情      企业黄页      同类资讯      网站地图      返回首页 迅博思语资讯移动站 http://mip.ksxb.net/ , 查看更多