There are several things awkward with this line of code:
phone_result= str(a).get_text().strip().encode("utf-8")
First of all, BeautifulSoup
works with unicode so in Python2 casting its text to str
is error prone. There's where I think is the mistake because even if the cast work, you're calling get_text()
to a str
object which will raise NameError
.
To end, you call encode
to the str
which in Python 2 is already encoded and it can potentially fail because Python 2 will decode it first (with a default encoding) and then encode it again.
So try with this fix assuming the web page is encoded in utf8
:
phone_result= a.get_text().strip().encode("utf-8")
There is also a problem with this line:
phone=soup.find("div", "phone-content")
find
will just return a single result, a Tag
object, you should better use find_all
which will return a list of Tag
objects. The difference is that when you iterate through the result of a single Tag
object you'll get NavigableString
s which doesn't have get_text
method. When you iterate through a list of Tag
objects you get Tag
objects in your iteration which have the get_text
method.
Hope this helps!