Aslıhan Yılmaz
Aslıhan Yılmaz

Reputation: 23

Beautiful soup module error(html parser)

I use beautifulsoup to find the number of pages on a webpage however when I write my code:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import urllib2
import requests
import BeautifulSoup


soup = BeautifulSoup(response.text)
pages = soup.select('div.pagination a')

a = int(pages[-2].text)
print a

It gives the following error:

Traceback (most recent call last): File "C:/Users/HangaarLab/Desktop/sonartik/sonartik.py", line 13, in soup = BeautifulSoup(response.text) TypeError: 'module' object is not callable

In another computer the code runs but it gives this warning: UserWarning: No parser was explicitly specified, so I'm using the best available HTML parser for this system ("html.parser"). This usually isn't a problem, but if you run this code on another system, or in a different virtual environment, it may use a different parser and behave differently.The code that caused this warning is on line 14 of the file C:/Users/Ar�elik/Desktop/sikayet/klo.py. To get rid of this warning, pass the additional argument 'features="html.parser"' to the BeautifulSoup constructor.

I need the code to work in the computer that gives the first error. What should I do?

Upvotes: 1

Views: 15000

Answers (3)

Arnav Chawla
Arnav Chawla

Reputation: 556

BeautifulSoup is part of the bs4 package. To fix your code simply do:

pip install bs4 

in the command line and change your import to:

from bs4 import BeautifulSoup

Upvotes: 0

Madhan Varadhodiyil
Madhan Varadhodiyil

Reputation: 2116

You'll have to import BeautifulSoup from bs4 package

import urllib2
import requests
from bs4 import BeautifulSoup #here
headers = {'User-Agent': 'Mozilla/5.0'}
response = requests.get("https://www.sikayetvar.com/onedio", headers = headers)

soup = BeautifulSoup(response.text)
pages = soup.select('div.pagination a')

a = int(pages[-2].text)
print a

Upvotes: 1

Rakesh
Rakesh

Reputation: 82755

Update

import BeautifulSoup

to

from bs4 import BeautifulSoup

Ex:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import urllib2
import requests
from bs4 import BeautifulSoup
headers = {'User-Agent': 'Mozilla/5.0'}
response = requests.get("https://www.sikayetvar.com/onedio", headers = headers)

soup = BeautifulSoup(response.text, "html.parser")   #Use a parser to fix second error warning 
pages = soup.select('div.pagination a')

a = int(pages[-2].text)
print a

Upvotes: 4

Related Questions