通过python实现Google的精准搜索功能
作者:WorkAgent
这篇文章主要介绍了通过python实现Google的精准搜索功能,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧
问题背景:
我想通过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%3A1715482705794&ei=UTBAZqCXMMvK1e8Pw_C8gAk&ved=0ahUKEwjgg7CKj4eGAxVLZfUHHUM4D5AQ4dUDCBE&uact=5&oq=%22%E6%96%B0%E8%83%BD%E6%BA%90%E6%B1%BD%E8%BD%A6%E7%94%B5%E6%B1%A0%22&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)
到此这篇关于通过python实现Google的精准搜索功能的文章就介绍到这了,更多相关python精准搜索内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!