Skip to content

Instantly share code, notes, and snippets.

@diije
Last active March 30, 2022 15:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save diije/96ac975f78082aae5a3c08af7713e4fd to your computer and use it in GitHub Desktop.
Save diije/96ac975f78082aae5a3c08af7713e4fd to your computer and use it in GitHub Desktop.
Simple SEO tests with Python, Requests and Requests-HTML
import requests
from requests_html import requests_html
if __name__ == '__main__':
# Define test data
test_data = [
{
"url": "https://www.example.com/",
"status": 200,
"title": "Example Domain",
"h1": "Example Domain",
},
]
for item in test_data:
try:
r = requests.get(item["url"])
html = HTML(html=r.text)
# No redirects
if r.history:
assert len(r.history) == 0, json.dumps({
"error_type": "redirection",
"url": item["url"],
"status": r.history[0].status_code,
})
# Only HTTP 200
assert r.status_code == 200, json.dumps({
"error_type": "not_http_200",
"url": item["url"],
"status": r.status_code,
})
# Assert title
assert len(html.xpath("//title")) == 1, json.dumps({
"error_type": "multiple_or_no_title",
"url": item["url"],
"nb_titles": len(html.xpath("//title")),
})
assert html.xpath("//title", first=True).text == item["title"], json.dumps({
"error_type": "wrong_title",
"url": item["url"],
"title": str(html.xpath("//title", first=True).text),
"expected_title": str(item["title"]),
})
# Assert h1
assert len(html.xpath("//h1")) == 1, json.dumps({
"error_type": "multiple_or_no_h1",
"url": item["url"],
"nb_h1": len(html.xpath("//h1")),
})
assert html.xpath("//h1", first=True).text == item["h1"], json.dumps({
"error_type": "wrong_h1",
"url": item["url"],
"h1": str(html.xpath("//h1", first=True).text),
"expected_h1": str(item["h1"]),
})
except AssertionError as e:
report.append(e)
continue
if len(report) > 0:
for e in report:
print(e)
else:
print("Everything's fine!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment