如果没记错,需要安装东西,引擎啥的
import time
from selenium import webdriver
from selenium.webdriver import ChromeOptions
from selenium.webdriver.common.by import By
from urllib.parse import urlencode
def get_url(keywords: list):
keyword = " ".join(keywords)
q1 = {"q1": keyword}
q1 = urlencode(q1)
url = f"https://www.baidu.com/s?{q1}&q2=&q3=&q4=&gpc=stf&ft=&q5=&q6=www.szgm.gov.cn&tn=baiduadv&ie=utf-8"
return url
# {text:url, }
def get_herf_text(url) -> dict:
option = ChromeOptions() # 隐藏webdriver提示条&自动化扩展信息,跳过验证
option.add_experimental_option('excludeSwitches',['enable-automation'])
option.add_experimental_option('useAutomationExtension',False)
# 无头模式访问
option.add_argument("--headless")
option.add_argument("--disable-gpu")
option.add_argument("--no-sandbox")
br = webdriver.Chrome(options = option)
br.execute_cdp_cmd(
"Page.addScriptToEvaluateOnNewDocument",
{'source':'Object.defineProperty(navigator,"webdriver",{get:()=>undefined})'})
br.get(url)
links = br.find_elements(By.XPATH,"//h3[@class='c-title t t tts-title']/a[contains(normalize-space(@class), '')]")
# 遍历每个链接元素,获取 href 和文本内容
result = {}
for link in links:
href = link.get_attribute("href")
text = link.text
result[text] = href
return result