Forums

A Beginner getting : 500 Internal Error need help

I am a beginner an was tying to create an API that scrapes data from a website and return it in json format. But when i open the server link it shows : Internal Server Error The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.

Here are my files:

Server.py

    from flask import Flask
from scrape import scrape

app = Flask(__name__)

@app.route("/")
def defaultData(country='india'):
    return scrape(country)

@app.route("/<country>")
def fetchdata(country='india'):
    return scrape(country)

scrape.py

    import json
import requests
from bs4 import BeautifulSoup

def scrape(country):
    country = country.lower().replace(' ', '-')
    res = requests.get('https://www.factmonster.com/world/countries/'+country)
    url_data = BeautifulSoup(res.text, 'html.parser')


    def data_heading_nolink(url_data):
        keys = ('Monetary unit', 'National name')
        result: dict = {}
        for j in keys:
            for i in url_data.find_all('p'):
                try:
                    if j in str(i):
                        result[j] = i.text.split(':')[1].strip()
                        break
                except IndexError as e:
                    result[j] = 'Failed to retrieve'
        return result


    def data_heading_link(url_data):
        keys = ('Capital', 'Language')
        result: dict = {}
        for j in keys:
            for i in url_data.find_all('p'):
                try:
                    if j in str(i):
                        result[j] = i.text.split(':')[1].split(',')[0].strip()
                        break
                    if 'Capital and largest' in str(i):
                        result['Largest city'] = result['Capital']
                except IndexError as e:
                    result[j] = 'Failed to retrieve'
        return result


    def get_map(url_data):
        map_code = str(url_data.select('.field_country_map')).split()
        for i in map_code:
            if 'src' in i:
                return('https://www.factmonster.com'+i[5:])
        return 'Map not found'


    info: dict = {}
    info.update(data_heading_nolink(url_data))
    info.update(data_heading_link(url_data))
    info['map']=get_map(url_data)

    counrty_json = json.dumps(info)
    return counrty_json

Error logs

    Traceback (most recent call last):
  File "/home/MyPythonServer608/.virtualenvs/my-virtualenv/lib/python3.10/site-packages/flask/app.py", line 2528, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/MyPythonServer608/.virtualenvs/my-virtualenv/lib/python3.10/site-packages/flask/app.py", line 1825, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/MyPythonServer608/.virtualenvs/my-virtualenv/lib/python3.10/site-packages/flask/app.py", line 1823, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/MyPythonServer608/.virtualenvs/my-virtualenv/lib/python3.10/site-packages/flask/app.py", line 1799, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
  File "/home/MyPythonServer608/country_data_fetching_API/server.py", line 12, in fetchdata
    return scrape(country)
  File "/home/MyPythonServer608/country_data_fetching_API/scrape.py", line 7, in scrape
    res = requests.get('https://www.factmonster.com/world/countries/'+country)
  File "/home/MyPythonServer608/.virtualenvs/my-virtualenv/lib/python3.10/site-packages/requests/api.py", line 73, in get
    return request("get", url, params=params, **kwargs)
  File "/home/MyPythonServer608/.virtualenvs/my-virtualenv/lib/python3.10/site-packages/requests/api.py", line 59, in request
    return session.request(method=method, url=url, **kwargs)
  File "/home/MyPythonServer608/.virtualenvs/my-virtualenv/lib/python3.10/site-packages/requests/sessions.py", line 587, in request
    resp = self.send(prep, **send_kwargs)
  File "/home/MyPythonServer608/.virtualenvs/my-virtualenv/lib/python3.10/site-packages/requests/sessions.py", line 701, in send
    r = adapter.send(request, **kwargs)
  File "/home/MyPythonServer608/.virtualenvs/my-virtualenv/lib/python3.10/site-packages/requests/adapters.py", line 559, in send
    raise ProxyError(e, request=request)
requests.exceptions.ProxyError: HTTPSConnectionPool(host='www.factmonster.com', port=443): Max retries exceeded with url: /world/countries/favicon.ico (Caused by ProxyError('Cannot connect to proxy.', OSError('Tunnel connection failed: 403 Forbidden')))

Server logs:

2023-02-25 14:27:59 [rule: 0] subject: path_info regexp: \.svgz$ action: addheader:Content-Encoding:gzip
2023-02-25 14:27:59 *** end of the internal routing table ***
2023-02-25 14:27:59 chdir() to /home/MyPythonServer608/
2023-02-25 14:27:59 your processes number limit is 128
2023-02-25 14:27:59 your memory page size is 4096 bytes
2023-02-25 14:27:59 detected max file descriptor number: 123456
2023-02-25 14:27:59 building mime-types dictionary from file /etc/mime.types...
2023-02-25 14:27:59 567 entry found
2023-02-25 14:27:59 lock engine: pthread robust mutexes
2023-02-25 14:27:59 thunder lock: disabled (you can enable it with --thunder-lock)
2023-02-25 14:27:59 uwsgi socket 0 bound to UNIX address /var/sockets/mypythonserver608.pythonanywhere.com/socket fd 3
2023-02-25 14:27:59 Python version: 3.10.5 (main, Jul 22 2022, 17:09:35) [GCC 9.4.0]
2023-02-25 14:27:59 PEP 405 virtualenv detected: /home/MyPythonServer608/.virtualenvs/my-virtualenv
2023-02-25 14:27:59 Set PythonHome to /home/MyPythonServer608/.virtualenvs/my-virtualenv
2023-02-25 14:27:59 *** Python threads support is disabled. You can enable it with --enable-threads ***
2023-02-25 14:27:59 Python main interpreter initialized at 0x55b37be84e60
2023-02-25 14:27:59 your server socket listen backlog is limited to 100 connections
2023-02-25 14:27:59 your mercy for graceful operations on workers is 60 seconds
2023-02-25 14:27:59 setting request body buffering size to 65536 bytes
2023-02-25 14:27:59 mapped 334256 bytes (326 KB) for 1 cores
2023-02-25 14:27:59 *** Operational MODE: single process ***
2023-02-25 14:27:59 initialized 38 metrics
2023-02-25 14:27:59 WSGI app 0 (mountpoint='') ready in 1 seconds on interpreter 0x55b37be84e60 pid: 1 (default app)
2023-02-25 14:27:59 *** uWSGI is running in multiple interpreter mode ***
2023-02-25 14:27:59 gracefully (RE)spawned uWSGI master process (pid: 1)
2023-02-25 14:27:59 spawned uWSGI worker 1 (pid: 2, cores: 1)
2023-02-25 14:27:59 metrics collector thread started
2023-02-25 14:27:59 spawned 2 offload threads for uWSGI worker 1
2023-02-25 14:28:00 announcing my loyalty to the Emperor...

www.factmonster.com is not on the allow list for free account. See https://www.pythonanywhere.com/whitelist/

ooh so only those sites are allowed to visit on free account ?

Correct