Reputation: 93
I am trying to compile the following code from parsing html but i come up with an error:
import string, urllib2, urlparse, csv, sys
from urllib import quote
from urlparse import urljoin
from bs4 import BeautifulSoup
from ast import literal_eval
changable_url = 'http://www.asusparts.eu/partfinder/Asus/All%20In%20One/E%20Series'
page = urllib2.urlopen(changable_url)
base_url = 'http://www.asusparts.eu'
soup = BeautifulSoup(page)
selects = []
redirects = []
model_info = []
#Opening csv writer
c = csv.writer(open(r"asus_stock.csv", "wb"))
#Object reader
cr = csv.reader(open(r"asus_stock.csv", "rb"))
print "FETCHING OPTIONS"
select = soup.find(id='myselectListModel')
selects.append(select)
print selects.get_text()
The error is:
print selects.get_text()
AttributeError: 'list' object has no attribute 'get_text'
How am i able to pass this error? Thanks.
Upvotes: 1
Views: 9669
Reputation: 1
You need get an element in a list.
Try selects[0].get_text()
and it should work.
Upvotes: -2
Reputation: 1573
I see this error a lot.
This has to do with what variable you are applying get.text()
to; get_text()
is a function that works with Beautiful soup. So you have to re-soup select:
select2 = BeautifulSoup(select, "lxml")
"lxml"
can be replaced by whatever parser you're using. If this doesn't work try turning select into a string first- and then doing the above variable function.
Upvotes: 1
Reputation: 5252
You are running the get_text()
function on selects
, which is a list. Lists don't have that function.
Should you not be running it on select
itself? Or what about each element in selects:
for item in selects:
print item.get_text()
Upvotes: 2