Aleksandar Ciric
Aleksandar Ciric

Reputation: 124

scraping data from a web page with python requests

I am trying to scrape domain search page (where you can enter the keyword, and get some random results) and I found this api url in network tab https://api.leandomainsearch.com/search?query=computer&count=all (for keyword: computer), but I am getting this error

{'error': True, 'message': 'Invalid API Credentials'}

here is the code

import requests

r = requests.get("https://api.leandomainsearch.com/search?query=cmputer&count=all")
print(r.json())

Upvotes: 1

Views: 185

Answers (1)

Andrej Kesely
Andrej Kesely

Reputation: 195438

The site needs that you set Authorization and Referer HTTP headers.

For example:

import re
import json
import requests


kw = 'computer'

url = 'https://leandomainsearch.com/search/'
api_url = 'https://api.leandomainsearch.com/search'

api_key = re.search(r'"apiKey":"(.*?)"', requests.get(url, params={'q': kw}).text)[1]
headers = {'Authorization': 'Key ' + api_key, 'Referer': 'https://leandomainsearch.com/search/?q={}'.format(kw)}
data = requests.get(api_url, params={'query': kw, 'count': 'all'}, headers=headers).json()

# uncomment this to print all data:
# print(json.dumps(data, indent=4))

for d in data['domains']:
    print(d['name'])

print()
print('Total:', data['_meta']['total_records'])

Prints:

...

blackopscomputer.com
allegiancecomputer.com
northpolecomputer.com
monumentalcomputer.com
fissioncomputer.com
hedgehogcomputer.com
blackwellcomputer.com
reflectionscomputer.com
towerscomputer.com
offgridcomputer.com
redefinecomputer.com
quantumleapcomputer.com

Total: 1727

Upvotes: 3

Related Questions