I’m trying to scrape a jail website using jupyter notebook and getting some errors. Can someone review this code and tell me how to fix the errors?
!pip install django requests beautifulsoup4
import csv
import requests
from bs4 import BeautifulSoup
from django.http import HttpResponse
from django.views import View
from django.conf import settings
Django app
from django.urls import path
from django.core.wsgi import get_wsgi_application
from django.http import HttpResponse
application = get_wsgi_application()
class InmateScraperView(View):
def get(self, request):
# Send a GET request to the website
url = ‘Inmate Lookup Tool’
response = requests.get(url)
# Parse the HTML content using BeautifulSoup
soup = BeautifulSoup(response.content, 'html.parser')
# Find the inmate table
table = soup.find('table', {'id': 'ctl00_ContentPlaceHolder1_grdIML'})
# Extract the table data
rows = table.find_all('tr')
data = []
for row in rows:
cells = row.find_all('td')
data.append([cell.text.strip() for cell in cells])
# Create a CSV response
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename="inmate_table.csv"'
# Write the data to the CSV file
writer = csv.writer(response)
writer.writerows(data)
return response
URL pattern
urlpatterns = [
path(‘scrape/’, InmateScraperView.as_view(), name=‘scrape’),
]
Run the Django app
from django.core.management import execute_from_command_line
execute_from_command_line([‘manage.py’, ‘runserver’])