May-29-2019, 04:47 AM
Alright, well been at the set_content_to_tag for hours now. With soup I have tried search() and find(), but I keep getting the error, well search gave me a different error so I switched back to find_all().
import requests, re from bs4 import BeautifulSoup from collections import Counter import statistics as stats import string #create your class here class TextAnalyzer(): def __init__(self, src, _content = 'none', _orig_content = 'none', _src_type = 'discover'): self.src = src self._src_type = _src_type self._content = _content self._orig_content = _orig_content if self._src_type == 'discover': if re.match(r"^http", src, re.I): self._src_type = 'url' elif re.findall(r"txt\Z", src, re.I): self._src_type = 'path' else: self._src_type = 'text' elif src == 'url': self._src_type = 'url' elif src == 'path': self._src_type = 'path' else: self._src_type = 'text' def set_content_to_tag(self, tag, tag_id = None): self.tag = tag self.tag_id = tag_id r = requests.get(url, tag) _orig_content = r.text soup = BeautifulSoup(_orig_content, 'html.parser') _content = soup.find_all(tag, tag_id)
Error:...F
======================================================================
FAIL: test_set_content_to_tag (__main__.TestTextAnalyzer)
----------------------------------------------------------------------
Traceback (most recent call last):
File "<ipython-input-27-3d60d3736c30>", line 23, in test_set_content_to_tag
self.assertEqual(ta._content[0:25], '\n\nAddress by Bill Clinton')
AssertionError: 'none' != '\n\nAddress by Bill Clinton'
- none
+
Address by Bill Clinton
----------------------------------------------------------------------
Ran 4 tests in 0.592s
FAILED (failures=1)