DataForSEO
DataForSeo provides comprehensive SEO and digital marketing data solutions via API.
The
DataForSeo API
retrievesSERP
from the most popular search engines likeBing
,Yahoo
. It also allows to >get SERPs from different search engine types likeMaps
,News
,Events
, etc.
This notebook demonstrates how to use theDataForSeo API to obtain search engine results.
%pip install--upgrade--quiet langchain-community
from langchain_community.utilities.dataforseo_api_searchimport DataForSeoAPIWrapper
Setting up the API credentials
You can obtain your API credentials by registering on theDataForSeo
website.
import os
os.environ["DATAFORSEO_LOGIN"]="your_api_access_username"
os.environ["DATAFORSEO_PASSWORD"]="your_api_access_password"
wrapper= DataForSeoAPIWrapper()
The run method will return the first result snippet from one of the following elements: answer_box, knowledge_graph, featured_snippet, shopping, organic.
wrapper.run("Weather in Los Angeles")
The Difference Betweenrun
andresults
run
andresults
are two methods provided by theDataForSeoAPIWrapper
class.
Therun
method executes the search and returns the first result snippet from the answer box, knowledge graph, featured snippet, shopping, or organic results. These elements are sorted by priority from highest to lowest.
Theresults
method returns a JSON response configured according to the parameters set in the wrapper. This allows for more flexibility in terms of what data you want to return from the API.
Getting Results as JSON
You can customize the result types and fields you want to return in the JSON response. You can also set a maximum count for the number of top results to return.
json_wrapper= DataForSeoAPIWrapper(
json_result_types=["organic","knowledge_graph","answer_box"],
json_result_fields=["type","title","description","text"],
top_count=3,
)
json_wrapper.results("Bill Gates")
Customizing Location and Language
You can specify the location and language of your search results by passing additional parameters to the API wrapper.
customized_wrapper= DataForSeoAPIWrapper(
top_count=10,
json_result_types=["organic","local_pack"],
json_result_fields=["title","description","type"],
params={"location_name":"Germany","language_code":"en"},
)
customized_wrapper.results("coffee near me")
Customizing the Search Engine
You can also specify the search engine you want to use.
customized_wrapper= DataForSeoAPIWrapper(
top_count=10,
json_result_types=["organic","local_pack"],
json_result_fields=["title","description","type"],
params={"location_name":"Germany","language_code":"en","se_name":"bing"},
)
customized_wrapper.results("coffee near me")
Customizing the Search Type
The API wrapper also allows you to specify the type of search you want to perform. For example, you can perform a maps search.
maps_search= DataForSeoAPIWrapper(
top_count=10,
json_result_fields=["title","value","address","rating","type"],
params={
"location_coordinate":"52.512,13.36,12z",
"language_code":"en",
"se_type":"maps",
},
)
maps_search.results("coffee near me")
Integration with Langchain Agents
You can use theTool
class from thelangchain.agents
module to integrate theDataForSeoAPIWrapper
with a langchain agent. TheTool
class encapsulates a function that the agent can call.
from langchain_core.toolsimport Tool
search= DataForSeoAPIWrapper(
top_count=3,
json_result_types=["organic"],
json_result_fields=["title","description","type"],
)
tool= Tool(
name="google-search-answer",
description="My new answer tool",
func=search.run,
)
json_tool= Tool(
name="google-search-json",
description="My new json tool",
func=search.results,
)
Related
- Toolconceptual guide
- Toolhow-to guides