Python脚本测试链接地址是否被谷歌收录

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("按任意键退出...")

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值