Search code examples
pythonweb-scrapingbeautifulsoupexport-to-csv

The following error pops when I try to scrape web data : module 'html5lib.treebuilders' has no attribute '_base'


I am trying out web scraping in python using beautiful soup, being a newbie took the source code from [https://syntaxbytetutorials.com/beautifulsoup-4-python-web-scraping-to-csv-excel-file/] and started experimenting. Now, I have an error

module 'html5lib.treebuilders' has no attribute '_base'`

It would be really helpful someone explained me the reason behind the error and provide a solution for it :)

import urllib.request as urllib
import csv
import re
from bs4 import BeautifulSoup
 
rank_page = 'https://socialblade.com/youtube/top/50/mostviewed'
request = urllib.Request(rank_page, headers={'User-Agent':'Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36'})
page = urllib.urlopen(request)
soup = BeautifulSoup(page, 'html.parser')
 
channels = soup.find('div', attrs={'style': 'float: right; width: 900px;'}).find_all('div', recursive=False)[4:]
 
file = open('topyoutubers.csv', 'wb')
writer = csv.writer(file)
 
# write title row
writer.writerow(['Username', 'Uploads', 'Views'])
 
for channel in channels:
    username = channel.find('div', attrs={'style': 'float: left; width: 350px; line-height: 25px;'}).a.text.strip()
    uploads = channel.find('div', attrs={'style': 'float: left; width: 80px;'}).span.text.strip()
    views = channel.find_all('div', attrs={'style': 'float: left; width: 150px;'})[1].span.text.strip()
 
    print (username + ' ' + uploads + ' ' + views)
    writer.writerow([username.encode('utf-8'), uploads.encode('utf-8'), views.encode('utf-8')])
 
file.close()

Solution

  • Try replacing the string "_base" by "base" in "_html5lib.py " .

    Can you show the traceback of your error ? that which line or file the error is coming from .