1

I hope that you are well. I have tried searching the other threads about my issue, but I have yet to find a solution. Attached below are the screenshots of my work and methods to install the bs4 module, and the error I encounter. I am more than willing to attach more screenshots if required. Here is the command prompt telling me that the installation was successful

import csv
import requests
from bs4 import BeautifulSoup4 as beauti

url = 'https://report.boonecountymo.org/mrcjava/servlet/SH01_MP.I00290s?max_rows=500'
response = requests.get(url, headers={'User-Agent': 'Mozilla/5.0'})
html = response.content

soup = BeautifulSoup(html)
table = soup.find('tbody', attrs={'class': 'stripe'})

list_of_rows = []
for row in table.findAll('tr'):
    list_of_cells = []
for cell in row.findAll('td'):
    text = cell.text.replace(' ', '')
    list_of_cells.append(text)
list_of_rows.append(list_of_cells)

outfile = open("./inmates.csv", "wb")
writer = csv.writer(outfile)
writer.writerow(["Last", "First", "Middle", "Gender", "Race", "Age", "City", "State"])
writer.writerows(list_of_rows)

Below is IDLE giving me the error:

Traceback (most recent call last):
  File "C:\Users\aditi\Desktop\webscraper.py", line 3, in <module>
    from bs4 import BeautifulSoup4 as beauti
ImportError: cannot import name 'BeautifulSoup4' from 'bs4' (C:\Users\aditi\AppData\Local\Programs\Python\Python38-32\lib\site-packages\bs4\__init__.py)
Aditya Das
  • 13
  • 3

2 Answers2

0

There's spelling mistake in the import statement!

It's BeautifulSoup and not BeautifulSoup4

    from bs4 import BeautifulSoup as beauti

EDIT

Also I would recommend to stick to what you import like,

  1. You are importing BeautifulSoup4 but in this line below, you are using BeautifulSoup() and not BeautifulSoup4()
    soup = BeautifulSoup(html)
  2. Importing BeautifulSoup4 as beauti and not using the variable 'beauti' anywhere.

We mostly stick with BeautifulSoup() and I also recommend you to do so.

I just found that this issue was previously asked and resolved here, check it out!

Ananth
  • 815
  • 1
  • 4
  • 11
  • I have tried using BeautifulSoup, yet it gives me the same error, I read that BeautifulSoup4 is for python versions above python 2 – Aditya Das Dec 27 '20 at 12:37
  • The url you have put up in the code is not working for me, can you check if it's accessible for you? – Ananth Dec 27 '20 at 12:58
  • 1
    Thanks for your help, you were right in the end aha! I followed what you said, and it worked for me. Once again, thank you Ananth :) – Aditya Das Dec 27 '20 at 18:34
0

There was a typo use BeautifulSoup instead of BeautifulSoup4.

import csv
import requests
from bs4 import BeautifulSoup

url = 'https://report.boonecountymo.org/mrcjava/servlet/SH01_MP.I00290s?max_rows=500'
response = requests.get(url, headers={'User-Agent': 'Mozilla/5.0'})
html = response.content

soup = BeautifulSoup(html, 'lxml')
table = soup.find('tbody', attrs={'class': 'stripe'})
# collect all rows except details and last row
total_items = [row.text.strip().split()[:-1] for row in table.find_all('tr')[:-1] ] # list of list

outfile = open("./inmates.csv", "w")
writer = csv.writer(outfile)

writer.writerow(["Last", "First", "Middle", "Gender", "Race", "Age", "City", "State"])
for row in total_items:
    writer.writerow(row)
Samsul Islam
  • 2,581
  • 2
  • 17
  • 23