当前位置: 首页>>代码示例>>Python>>正文


Python Scraper.extractTag方法代码示例

本文整理汇总了Python中scraper.Scraper.extractTag方法的典型用法代码示例。如果您正苦于以下问题:Python Scraper.extractTag方法的具体用法?Python Scraper.extractTag怎么用?Python Scraper.extractTag使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在scraper.Scraper的用法示例。


在下文中一共展示了Scraper.extractTag方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: testExtractTag

# 需要导入模块: from scraper import Scraper [as 别名]
# 或者: from scraper.Scraper import extractTag [as 别名]
 def testExtractTag(self):
     pattern = "<a name='$name'></a>"
     _scraper = Scraper(pattern)
     exp = BeautifulSoup(pattern)
     
     # one attribute
     actual = BeautifulSoup("<a name='abc'></a>")
     self.assertEqual('abc', _scraper.extractTag(exp.contents[0], actual.contents[0])['name'])
     
     # one attribute
     actual = BeautifulSoup("<a name='abc' age='27'></a>")
     self.assertEqual('abc', _scraper.extractTag(exp.contents[0], actual.contents[0])['name'])
     
     # two attributes
     pattern = "<a name='$name' age='$age'></a>"
     exp = BeautifulSoup(pattern)
     actual = BeautifulSoup("<a name='abc' age='27'></a>")
     ret =  _scraper.extractTag(exp.contents[0], actual.contents[0])
     self.assertEqual(2, len(ret))
     self.assertEqual('abc', ret['name'])
     self.assertEqual('27', ret['age'])
     
     # get attribute from sub tag
     pattern = "<a><b name='$name'></b></a>"
     exp = BeautifulSoup(pattern)
     
     # one attribute
     actual = BeautifulSoup("<a><b name='abc'></b></a>")
     self.assertEqual('abc', _scraper.extractTag(exp.contents[0], actual.contents[0])['name'])
开发者ID:Zacchy,项目名称:nickcheng-python,代码行数:31,代码来源:scraper_test.py

示例2: testExtractAsteriskValue

# 需要导入模块: from scraper import Scraper [as 别名]
# 或者: from scraper.Scraper import extractTag [as 别名]
    def testExtractAsteriskValue(self):
        pattern = "<a>*$content</a>"
        _scraper = Scraper(pattern)
        exp = BeautifulSoup(pattern)
        
        # extract text
        actual = BeautifulSoup("<a>hello world</a>")
        self.assertTrue(_scraper.matchTag(exp.contents[0], actual.contents[0]))
        self.assertEqual('hello world', _scraper.extractTag(exp.contents[0], actual.contents[0])['content'][0])        

        pattern = "<a>*(b)$content</a>"
        _scraper = Scraper(pattern)    
        exp = BeautifulSoup(pattern)
        
        # asterisk only restrict on tag but not text
        actual = BeautifulSoup("<a>hello world</a>")
        self.assertTrue(_scraper.matchTag(exp.contents[0], actual.contents[0]))
        self.assertEqual('hello world', _scraper.extractTag(exp.contents[0], actual.contents[0])['content'][0])        
        
        # asterisk restrict tag
        actual = BeautifulSoup("<a><c></c></a>")
        self.assertFalse(_scraper.matchTag(exp.contents[0], actual.contents[0]))
        
        # asterisk restrict tag
        actual = BeautifulSoup("<a><b>hello world</b></a>")
        self.assertTrue(_scraper.matchTag(exp.contents[0], actual.contents[0]))
        self.assertEqual(BeautifulSoup('<b>hello world</b>').contents[0], _scraper.extractTag(exp.contents[0], actual.contents[0])['content'][0])
        
        # asterisk restrict tag
        actual = BeautifulSoup("<a><b>hello</b><b>world</b></a>")
        self.assertTrue(_scraper.matchTag(exp.contents[0], actual.contents[0]))
        ret = _scraper.extractTag(exp.contents[0], actual.contents[0])
        self.assertEqual(BeautifulSoup('<b>hello</b>').contents[0], ret['content'][0])        
        self.assertEqual(BeautifulSoup('<b>world</b>').contents[0], ret['content'][1])
        
        # prefix asterisk
        pattern = "<a>*(b)<c></c>$content</a>"
        _scraper = Scraper(pattern)    
        exp = BeautifulSoup(pattern)
        
        actual = BeautifulSoup("<a><b></b><b></b><c></c>hello world</a>")
        self.assertTrue(_scraper.matchTag(exp.contents[0], actual.contents[0]))
        ret = _scraper.extractTag(exp.contents[0], actual.contents[0])
        self.assertEqual('hello world', ret['content'])

        # prefix asterisk
        pattern = "<a>*(b)<c></c>*$content</a>"
        _scraper = Scraper(pattern)    
        exp = BeautifulSoup(pattern)
        
        actual = BeautifulSoup("<a><b></b><c></c><d>hello world</d></a>")
        self.assertTrue(_scraper.matchTag(exp.contents[0], actual.contents[0]))
        ret = _scraper.extractTag(exp.contents[0], actual.contents[0])
        self.assertEqual(BeautifulSoup('<d>hello world</d>').contents[0], ret['content'][0])
        
        actual = BeautifulSoup("<a><b></b><c></c><d>hello</d><d>world</d></a>")
        self.assertTrue(_scraper.matchTag(exp.contents[0], actual.contents[0]))
        ret = _scraper.extractTag(exp.contents[0], actual.contents[0])
        self.assertEqual(BeautifulSoup('<d>hello</d>').contents[0], ret['content'][0])
        self.assertEqual(BeautifulSoup('<d>world</d>').contents[0], ret['content'][1])
        
        # prefix asterisk
        pattern = "<a>*<c></c>*$content</a>"
        _scraper = Scraper(pattern)    
        exp = BeautifulSoup(pattern)
        
        actual = BeautifulSoup("<a><b></b>some text<c></c><d>hello world</d></a>")
        self.assertTrue(_scraper.matchTag(exp.contents[0], actual.contents[0]))
        ret = _scraper.extractTag(exp.contents[0], actual.contents[0])
        
        self.assertEqual(BeautifulSoup('<d>hello world</d>').contents[0], ret['content'][0])
        
开发者ID:Zacchy,项目名称:nickcheng-python,代码行数:73,代码来源:scraper_test.py


注:本文中的scraper.Scraper.extractTag方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。