Installation of BS4 successful, python not recognising

Viewed 73

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)
2 Answers

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!

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)
Related