欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

DataForSeo serp api 使用

程序员文章站 2022-05-16 19:52:34
...

serp 使用搜索引擎结果页
由于谷歌搜索引擎反爬太厉害。
我给大家推荐一个好用的平台:
DataForSeo serp api
serp 提供前100 的搜索引擎结果指定关键字,搜索引擎和搜索位置的实时数据。
所有的post 数据均以json 格式发送
平台地址:
https://docs.dataforseo.com/v3/serp/google/organic/live/regular/?python
使用python 语言操作

脚本1

from http.client import HTTPSConnection
from base64 import b64encode
from json import loads
from json import dumps

class RestClient:
    domain = "api.dataforseo.com"

    def __init__(self, username, password):
        self.username = username
        self.password = password

    def request(self, path, method, data=None):
        connection = HTTPSConnection(self.domain)
        try:
            base64_bytes = b64encode(
                ("%s:%s" % (self.username, self.password)).encode("ascii")
                ).decode("ascii")
            headers = {'Authorization' : 'Basic %s' %  base64_bytes, 'Content-Encoding' : 'gzip'}
            connection.request(method, path, headers=headers, body=data)
            response = connection.getresponse()
            return loads(response.read().decode())
        finally:
            connection.close()

    def get(self, path):
        return self.request(path, 'GET')

    def post(self, path, data):
        if isinstance(data, str):
            data_str = data
        else:
            data_str = dumps(data)
        return self.request(path, 'POST', data_str)

脚本2

from client import RestClient
client = RestClient("login", "password")
post_data = dict()
# You can set only one task at a time
post_data[len(post_data)] = dict(
    language_code="en",
    location_code=2840,
    keyword="albert einstein"
)
# POST /v3/serp/google/organic/live/regular

response = client.post("/v3/serp/google/organic/live/regular", post_data)
if response["status_code"] == 20000:
    print(response)
    # do something with result
else:
    print("error. Code: %d Message: %s" % (response["status_code"], response["status_message"]))

获取结果
DataForSeo serp api 使用

相关标签: seo