问题背景:
我想通过google或者其他网站通过精准搜索确认该产品是否存在,但是即使该产品不存在google也会返回一些相关的url链接,现在想通过python实现搜索结果的精准匹配以确认该产品是否为正确的名称【可以通过google搜索到,如果搜索不到则认为该产品不存在】,以下为精准结果截图
实现代码:
import requests from bs4 import beautifulsoup def is_product(product): query = product.replace(' ', ' ') query = '"' query '"' add = '&sca_esv=396701017a0fe9d3&sca_upv=1&sxsrf=adlywikwgdkr0hofoscsrshq3fr-z5vdma:1715482705794&ei=utbazqcxmmvk1e8pw_c8gak&ved=0ahukewjgg7ckj4egaxvlzfuhhum4d5aq4dudcbe&uact=5&oq="新能源汽车电池"&gs_lp=egxnd3mtd2l6lxnlcnaifylmlrdog73mupdmsb3ovabnllxmsaaimgyqabgega8ybhaagb4ydzigeaayhhgpmggqabiabbiibdiieaaygaqyogqycbaagiaegkiesp8fuabyahaaeacqaqcyaeiboahiaaobazitmbgba8gbapgbavgbazgcaaac5qgyawcsbwmyltggb8kc&sclient=gws-wiz-serp' url = f"https://www.google.com/search?q={query}&as_q={query}&tbs=li:1" print(url) headers = { "user-agent": "mozilla/5.0 (windows nt 10.0; win64; x64) applewebkit/537.36 (khtml, like gecko) chrome/91.0.4472.124 safari/537.36" } resp = requests.get(url, headers=headers) decoded_text = resp.text # print(">>>" * 20) # print(decoded_text) # print(">>>" * 20) results = [] if resp.status_code == 200: soup = beautifulsoup(resp.content, "html.parser") # print(soup) for g in soup.find_all('div', class_='tf2cxc'): title = g.find('h3').text link = g.find('a')['href'] item = { "title": title, "link": link } results.append(item) print(results) else: print("failed to fetch search results") return true if len(results)>=1 else false query = '"新能源汽车电池"' query = '"高档数控机床用变频智能电动执行器(电动夹爪)"' query = '"cae—多学科设计集成与优化"' res = [] for query in ["新能源汽车电池","高档数控机床用变频智能电动执行器(电动夹爪)","cae—多学科设计集成与优化"]: res.append(is_product(query)) print(res)