Mar-09-2019, 01:17 AM
Good evening,
When running my code, it is giving me an error that I am not understanding as it has lines referenced that I don't have. My code only has 234 lines, but the error is referencing things that I do not have listed in my code. How do I figure out what the problem is?
This is where it is saying my error is. But I am not sure why:
When running my code, it is giving me an error that I am not understanding as it has lines referenced that I don't have. My code only has 234 lines, but the error is referencing things that I do not have listed in my code. How do I figure out what the problem is?
This is where it is saying my error is. But I am not sure why:
def set_content_to_tag(self, tag, tag_id=None): """Changes _content to the text within a specific element of an HTML document. Keyword arguments: tag (str) -- Tag to read tag_id (str) -- ID of tag to read It's possible the HTML does not contain the tag being searched. You should use exception handling to catch any errors.""" soup = BeautifulSoup(self._content, 'html.parser') content = soup.find('{}'.format(tag),{'id':'{}'.format(tag_id)}) if content == None: raise Exception ("Tag or attribute does not exist") self._content = content.getText() print(content)This is the error it shows, and I am completely lost. Can someone help me understand why it is referencing lines of code I don't have, and how it got there?
Error:TypeError Traceback (most recent call last)
<ipython-input-3-6ba650ec7019> in <module>()
218 print(ta._orig_content)
219 print(ta._orig_content)
--> 220 ta.set_content_to_tag('div','content-main')
221 ta.set_content_to_tag('div' ,'device-xs visible-xs')
222 print(ta._content)
<ipython-input-3-6ba650ec7019> in set_content_to_tag(self, tag, tag_id)
58 You should use exception handling to catch any errors."""
59
---> 60 soup = BeautifulSoup(self._content, 'html.parser')
61 content = soup.find('{}'.format(tag),{'id':'{}'.format(tag_id)})
62 if content == None:
~\Anaconda3\lib\site-packages\bs4\__init__.py in __init__(self, markup, features, builder, parse_only, from_encoding, exclude_encodings, **kwargs)
277 self.contains_replacement_characters) in (
278 self.builder.prepare_markup(
--> 279 markup, from_encoding, exclude_encodings=exclude_encodings)):
280 self.reset()
281 try:
~\Anaconda3\lib\site-packages\bs4\builder\_htmlparser.py in prepare_markup(self, markup, user_specified_encoding, document_declared_encoding, exclude_encodings)
235 try_encodings = [user_specified_encoding, document_declared_encoding]
236 dammit = UnicodeDammit(markup, try_encodings, is_html=True,
--> 237 exclude_encodings=exclude_encodings)
238 yield (dammit.markup, dammit.original_encoding,
239 dammit.declared_html_encoding,
~\Anaconda3\lib\site-packages\bs4\dammit.py in __init__(self, markup, override_encodings, smart_quotes_to, is_html, exclude_encodings)
364
365 u = None
--> 366 for encoding in self.detector.encodings:
367 markup = self.detector.markup
368 u = self._convert_from(encoding)
~\Anaconda3\lib\site-packages\bs4\dammit.py in encodings(self)
255 if self.declared_encoding is None:
256 self.declared_encoding = self.find_declared_encoding(
--> 257 self.markup, self.is_html)
258 if self._usable(self.declared_encoding, tried):
259 yield self.declared_encoding
~\Anaconda3\lib\site-packages\bs4\dammit.py in find_declared_encoding(cls, markup, is_html, search_entire_document)
313
314 declared_encoding = None
--> 315 declared_encoding_match = xml_encoding_re.search(markup, endpos=xml_endpos)
316 if not declared_encoding_match and is_html:
317 declared_encoding_match = html_meta_re.search(markup, endpos=html_endpos)
TypeError: expected string or bytes-like object