我们从Python开源项目中,提取了以下19个代码示例,用于说明如何使用bs4.element.Comment()。
def comment(self, content): "Handle comments as Comment objects." self.soup.endData() self.soup.handle_data(content) self.soup.endData(Comment)
def is_comment(self): ''' Check if this element is a comment ''' return isinstance(self.context, Comment)
def test_cdata_where_it_doesnt_belong(self): # Random CDATA sections are converted into comments. markup = "<div><![CDATA[foo]]>" soup = self.soup(markup) data = soup.find(text="[CDATA[foo]]") self.assertEqual(data.__class__, Comment)
def test_incomplete_declaration(self): # An incomplete declaration is treated as a comment. markup = 'a<!b <p>c' self.assertSoupEquals(markup, "a<!--b <p-->c") # Let's spell that out a little more explicitly. soup = self.soup(markup) str1, comment, str2 = soup.body.contents self.assertEqual(str1, 'a') self.assertEqual(comment.__class__, Comment) self.assertEqual(comment, 'b <p') self.assertEqual(str2, 'c')
def test_document_starts_with_bogus_declaration(self): soup = self.soup('<! Foo >a') # 'Foo' becomes a comment that appears before the HTML. comment = soup.contents[0] self.assertTrue(isinstance(comment, Comment)) self.assertEqual(comment, 'Foo') self.assertEqual(self.find(text="a") == "a")
def test_document_starts_with_bogus_declaration(self): soup = self.soup('<! Foo ><p>a</p>') # The declaration becomes a comment. comment = soup.contents[0] self.assertTrue(isinstance(comment, Comment)) self.assertEqual(comment, ' Foo ') self.assertEqual(soup.p.string, 'a')
def test_document_ends_with_incomplete_declaration(self): soup = self.soup('<p>a<!b') # This becomes a string 'a'. The incomplete declaration is ignored. # Compare html5lib, which turns it into a comment. s, comment = soup.p.contents self.assertEqual(s, 'a') self.assertTrue(isinstance(comment, Comment)) self.assertEqual(comment, 'b')