repl.it
@Scoder12/

wa api

Python

No description

fork
loading
Files
  • main.py
  • Packager files
  • poetry.lock
  • pyproject.toml
  • requirements.txt
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import requests
from urllib.parse import quote
from bs4 import BeautifulSoup

def check(val):
    if not val:
        raise AssertionError("Assertion failed")


class WAUnknownError(Exception): pass


def wa_query(inp):
    r = requests.get(
        "https://www.wolframalpha.com/widget/input/?input=" + quote(inp) + "&id=30bb931d996e9706032d4a13d1787f12"
    )
    r.raise_for_status()
    soup = BeautifulSoup(r.text, 'html.parser')
    result_div = soup.find(id="scannerresult_0200_1") or soup.find(id="scannerresult_0300_1")
    if result_div is None:
        raise WAUnknownError("Unknown")
    result_img = result_div.find("img")
    check(result_img is not None)
    check("alt" in result_img.attrs)
    return result_img.attrs["alt"]

inp = input("Enter query: ")
res = wa_query(inp)
print(res)
Fetching token
?