Python脚本测试链接地址是否被谷歌收录
源码:测试单条链接
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
import time
# 设置Chrome选项
options = webdriver.ChromeOptions()
options.add_argument("--headless") # 启用无头模式(不弹出浏览器界面)
# 初始化浏览器驱动
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
def check_google_search_for_card_section(target_url):
# 打开谷歌搜索页面
driver.get("https://www.google.com/")
# 找到搜索框并输入目标地址
search_box = driver.find_element(By.NAME, "q")
search_box.send_keys(target_url)
search_box.send_keys(Keys.RETURN)
# 等待页面加载
time.sleep(3)
try:
# 查找是否有class为card-section的div
elements = driver.find_elements(By.CSS_SELECTOR, "div.card-section")
if elements:
print("未收录")
else:
print("已收录")
except Exception as e:
print("Error:", e)
finally:
driver.quit()
# 输入目标网址
target_url = input("请输入目标网址: ")
check_google_search_for_card_section(target_url)
源码:测试多条链接
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
import time
# 设置Chrome选项
options = webdriver.ChromeOptions()
options.add_argument("--headless") # 启用无头模式(不弹出浏览器界面)
# 初始化浏览器驱动
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
def check_google_search_for_card_section(target_url):
# 打开谷歌搜索页面
driver.get("https://www.google.com/")
# 找到搜索框并输入目标地址
search_box = driver.find_element(By.NAME, "q")
search_box.send_keys(target_url)
search_box.send_keys(Keys.RETURN)
# 等待页面加载
time.sleep(3)
try:
# 查找是否有class为card-section的div
elements = driver.find_elements(By.CSS_SELECTOR, "div.card-section")
if elements:
print(f"{target_url} - 未收录")
else:
print(f"{target_url} - 已收录")
except Exception as e:
print("Error:", e)
def process_multiple_addresses():
# 获取用户输入的多个地址
print("请输入多个网址,按回车确认每个网址,输入完毕后输入 'done' 来结束:")
urls = []
while True:
url = input("请输入目标网址: ")
if url.lower() == 'done':
break
urls.append(url)
# 处理每个地址
for url in urls:
check_google_search_for_card_section(url)
# 运行程序
process_multiple_addresses()
input("按任意键退出...")