python - "TypeError: expected string or buffer" with BeautifulSoup -
i relatively new python , beautifulsoup. need parse response of request using urllib2 (for request , response) , beautifulsoup4 parsing.
i have used these without problems. however, particular project strangely getting errors.
below part of code have written :
class weblogin(object): def __init__(self, username, password, targetsite, loginurl): # url website want log in self.base_url = targetsite; self.loginurl = self.base_url + loginurl; # user supplied username , password self.username = username; self.password = password; # file storing cookies self.cookie_file = 'login.cookies' # set cookie jar store cookies self.cj = cookielib.mozillacookiejar(self.cookie_file) # set opener handle cookies, redirects etc self.opener = urllib2.build_opener( urllib2.httpredirecthandler(), urllib2.httphandler(debuglevel=0), urllib2.httpshandler(debuglevel=0), urllib2.httpcookieprocessor(self.cj) ); # pretend we're web browser , not python script self.opener.addheaders = [ ('user-agent', 'mozilla/5.0 (macintosh; intel mac os x 10.9; rv:35.0) gecko/20100101 firefox/35.0') ]; # open front page of website set , save initial cookies , retrieve csrf token (__fk csrf token here) required login response = self.opener.open(self.base_url); self.cj.save(); print response.info().get('content-encoding'); initialresponse = response.read(); print "\nresponse received \n\n",initialresponse; # parsing response csrf token soup = beautifulsoup(initialresponse);
now when printing print "\nresponse received \n\n",initialresponse; above, proper html response. however, when trying soup = beautifulsoup(initialresponse); above, following error:
please let me know, going wrong here ? missing ? why not able make soup out of response.read() ?
i have tried using .decode('utf-8') in case fiddling around things, hasn't helped situation.
here error again in case above snapshot not legible :
traceback (most recent call last):
file "flipkartlogin.py", line 64, in weblogin(username, password, targetsite, loginurl);
file "flipkartlogin.py", line 43, in init soup = beautifulsoup(initialresponse);
file "/library/python/2.7/site-packages/bs4/init.py", line 172, in init self._feed()
file "/library/python/2.7/site-packages/bs4/init.py", line 185, in _feed self.builder.feed(self.markup)
file "/library/python/2.7/site-packages/bs4/builder/_htmlparser.py", line 146, in feed parser.feed(markup)
file "/system/library/frameworks/python.framework/versions/2.7/lib/python2.7/htmlparser.py", line 114, in feed self.goahead(0)
file "/system/library/frameworks/python.framework/versions/2.7/lib/python2.7/htmlparser.py", line 158, in goahead k = self.parse_starttag(i)
file "/system/library/frameworks/python.framework/versions/2.7/lib/python2.7/htmlparser.py", line 324, in parse_starttag self.handle_starttag(tag, attrs)
file "/library/python/2.7/site-packages/bs4/builder/_htmlparser.py", line 48, in handle_starttag self.soup.handle_starttag(name, none, none, dict(attrs))
file "/library/python/2.7/site-packages/bs4/init.py", line 298, in handle_starttag self.currenttag, self.previous_element)
file "/library/python/2.7/site-packages/bs4/element.py", line 749, in init self.name, attrs)
file "/library/python/2.7/site-packages/bs4/builder/init.py", line 160, in _replace_cdata_list_attribute_values values = whitespace_re.split(value) typeerror: expected string or buffer
Comments
Post a Comment