爱采购企业在百度首页的阿拉丁位有机会得到展示,固定8个位置,前提是百度认为这个关键词是产品关键词。
工具用来查询有多少自己爱采购店铺的商品在首页阿拉丁位展示了。
下面直接贴代码,写的很low,但能用,还是那句毕竟不准备转行,见谅。
8月26日更新:查询结果写入excel表的pc和mobile列
固定cookie会导致查询几百个之后获取不到页面,需要重新设置cookie,代码新增所执行到ID的提示,可以直接改写count数值继续查询
PC和移动端的脚本不能同时运行,运行时excel不能打开
PC端百度首页阿拉丁位查询:
import requests
import re
import time
import xlrd
import xlwt
from xlutils.copy import copy
file_location = "./keywords.xls" //xls格式的excel表一张四列,id,keyword,pc,mobile
data = xlrd.open_workbook(file_location)
sheet = data.sheet_by_index(0)
workbook = xlrd.open_workbook(file_location)
new_workbook = copy(workbook)
sheets = new_workbook.get_sheet(0)
cookies = '填写自己的cookie'
headers = {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "zh-CN,zh;q=0.9",
"Cache-Control": "max-age=0",
"Connection": "keep-alive",
"DNT": "1",
"Host": "www.baidu.com",
"Referer": "https://www.baidu.com/",
"sec-ch-ua-mobile": "?0",
"Sec-Fetch-Dest": "document",
"Sec-Fetch-Mode": "navigate",
"Sec-Fetch-Site": "same-origin",
"Sec-Fetch-User": "?1",
"Upgrade-Insecure-Requests": "1",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36",
"cookie": cookies
}
count = 1
while count < sheet.nrows:
keyword = sheet.cell_value(count, 1)
company = "公司名"
#print("正在查询",keyword)
# 获取搜索网址
search_urls = []
search_url = f"https://www.baidu.com/s?wd={keyword}"
search_urls.append(search_url)
req = requests.get(search_url, headers=headers, timeout=10).text
search_contentze = r'<div id="content_left">(.+?)<div style="clear:both;height:0;"></div>'
search_content = re.findall(search_contentze, req, re.S)
search_content = search_content[0]
search_objectze = r'srcid="5103"(.+?)<div><span class="c-showurl"></span></div>'
search_object = re.findall(search_objectze, search_content, re.S)
search_objects = search_object
#print("百度搜索结果页:", search_objects)
seach_data = ''
companysze = r'<p class="op-b2b-product-item-address c-color-gray2 c-gap-top-mini" title="(.+?)">'
search_objects = str(search_objects)
companys = re.findall(companysze, search_objects, re.S)
if company in companys:
print("编号",count,"关键词",keyword, "在百度首页阿拉丁位")
sheets.write(count, 2, "阿拉丁位")
new_workbook.save("keywords.xls")
else:
print("编号",count,"关键词",keyword, "不在百度首页阿拉丁位")
sheets.write(count, 2, "不在阿拉丁位或该关键词没有阿拉丁展现")
new_workbook.save("keywords.xls")
time.sleep(3) # 设置延迟间隔
count = count + 1
new_workbook.save("keywords.xls")
移动端百度首页阿拉丁位查询:
from typing import List
import requests
import re
import time
import xlrd
import xlwt
from xlutils.copy import copy
file_location = "./keywords.xls"
data = xlrd.open_workbook(file_location)
sheet = data.sheet_by_index(0)
workbook = xlrd.open_workbook(file_location)
new_workbook = copy(workbook)
sheets = new_workbook.get_sheet(0)
cookies = '自己获取的cookie'
headers = {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "zh-CN,zh;q=0.9",
"Cache-Control": "max-age=0",
"Connection": "keep-alive",
"Host": "www.baidu.com",
"Sec-Fetch-Dest": "document",
"Sec-Fetch-Mode": "navigate",
"Sec-Fetch-Site": "none",
"Sec-Fetch-User": "?1",
"Upgrade-Insecure-Requests": "1",
"User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1",
"cookie": cookies
}
count = 1
while count < sheet.nrows:
keyword = sheet.cell_value(count, 1)
company = "公司名"
#print("正在查询",keyword)
# 获取搜索网址
search_urls = []
search_url = f"https://m.baidu.com/s?wd={keyword}"
search_urls.append(search_url)
req = requests.get(search_url, headers=headers, timeout=10).text
search_contentze = r'<div id="page-bd" class="se-page-bd first-page" ssrt="n">(.+?)<div id="page-relative" class="se-page-relative">'
search_content = re.findall(search_contentze, req, re.S)
search_content = search_content[0]
search_objectze = r'<div class="c-result result"(.+?)<span data-a-1ab54a4d class="text">查看更多</span>'
search_object = re.findall(search_objectze, search_content, re.S)
search_objects = search_object
seach_data = ''
companysze = r'<div class="c-line-clamp1" style="text-align:left;"><!---->(.+?)</div>'
search_objects = str(search_objects)
companys = re.findall(companysze, search_objects, re.S)
if company in companys:
print("编号",count,"关键词",keyword, "在百度首页阿拉丁位")
sheets.write(count, 3, "阿拉丁位")
new_workbook.save("keywords.xls")
else:
print("编号",count,"关键词",keyword, "不在百度首页阿拉丁位")
sheets.write(count, 3, "不在阿拉丁位或该关键词没有阿拉丁展现")
new_workbook.save("keywords.xls")
time.sleep(3) # 设置延迟间隔
count = count + 1
new_workbook.save("keywords.xls")
效果如下:
编号 1 关键词 阻燃乙烯基树脂905 在百度首页阿拉丁位
编号 2 关键词 阻燃乙烯基树脂 不在百度首页阿拉丁位
编号 3 关键词 阻燃拉挤树脂 在百度首页阿拉丁位
编号 4 关键词 阻燃胶衣 不在百度首页阿拉丁位
编号 5 关键词 阻燃玻纤布 不在百度首页阿拉丁位
编号 6 关键词 阻燃玻璃纤维布 不在百度首页阿拉丁位
编号 7 关键词 中温高温固化剂 在百度首页阿拉丁位
编号 8 关键词 中碱短切毡 在百度首页阿拉丁位
编号 9 关键词 中碱玻纤布 不在百度首页阿拉丁位
编号 10 关键词 中碱玻璃纤维无捻粗纱布 在百度首页阿拉丁位
编号 11 关键词 中碱玻璃纤维布04布 在百度首页阿拉丁位
编号 12 关键词 中碱玻璃纤维布 不在百度首页阿拉丁位
编号 13 关键词 中碱玻璃纤维表面毡 在百度首页阿拉丁位
编号 14 关键词 帐篷杆树脂 不在百度首页阿拉丁位
编号 15 关键词 渔船树脂 在百度首页阿拉丁位
编号 16 关键词 游艇树脂 在百度首页阿拉丁位
编号 17 关键词 游艇胶衣树脂 不在百度首页阿拉丁位
编号 18 关键词 油罐树脂 不在百度首页阿拉丁位
编号 19 关键词 引发剂OT 在百度首页阿拉丁位
编号 20 关键词 引发剂O 不在百度首页阿拉丁位
编号 21 关键词 引发剂C 在百度首页阿拉丁位
编号 22 关键词 引发剂BPO 不在百度首页阿拉丁位
编号 23 关键词 异辛酸钴溶液 不在百度首页阿拉丁位
编号 24 关键词 乙烯基阻燃树脂 在百度首页阿拉丁位
编号 25 关键词 乙烯基酯树脂防腐 不在百度首页阿拉丁位
编号 26 关键词 乙烯基酯树脂901 在百度首页阿拉丁位
编号 27 关键词 乙烯基树脂消泡剂 在百度首页阿拉丁位
编号 28 关键词 乙烯基树脂耐温 在百度首页阿拉丁位
编号 29 关键词 乙烯基树脂耐高温度 不在百度首页阿拉丁位
编号 30 关键词 乙烯基树脂胶衣 在百度首页阿拉丁位
编号 31 关键词 乙烯基树脂固化剂 不在百度首页阿拉丁位
编号 32 关键词 乙烯基树脂促进剂 不在百度首页阿拉丁位
编号 33 关键词 乙烯基树脂报价 不在百度首页阿拉丁位
编号 34 关键词 乙烯基树脂907 在百度首页阿拉丁位
编号 35 关键词 乙烯基树脂901 不在百度首页阿拉丁位
编号 36 关键词 乙烯基树脂 不在百度首页阿拉丁位
编号 37 关键词 乙烯基促进剂 不在百度首页阿拉丁位
因为爱采购这个位置对普通商家变动频率实在太大,也没看到ID标记排名,所以没写拍第几,后期可能加上排名。